如果我正在寻找关键字“sales”,即使文件中有多个链接,我也希望得到最近的“http://www.somewebsite.com”.我想最近的链接不是第一个链接.这意味着我需要搜索关键字匹配之前的链接.
这不起作用……
regex =(http | https):// [-A-Za-z0-9./].*(?!((http | https):// [-A-Za-z0-9./]))销售
销售
什么是找到最接近关键字的链接的最佳方法?
最佳答案 使用
HTML解析器而不是正则表达式通常更容易,更健壮.
使用第三方模块lxml:
import lxml.html as LH
content = '''<html><a href="http://www.not-this-one.com"></a>
<a href="http://www.somewebsite.com"></a><p>other stuff</p><p>sales</p>
</html>
'''
doc = LH.fromstring(content)
for url in doc.xpath('''
//*[contains(text(),"sales")]
/preceding::*[starts-with(@href,"http")][1]/@href'''):
print(url)
产量
http://www.somewebsite.com
我发现lxml(和XPath)是表达我正在寻找的元素的便捷方式.但是,如果不能选择安装第三方模块,则还可以使用标准库中的HTMLParser完成此特定作业:
import HTMLParser
import contextlib
class MyParser(HTMLParser.HTMLParser):
def __init__(self):
HTMLParser.HTMLParser.__init__(self)
self.last_link = None
def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
if 'href' in attrs:
self.last_link = attrs['href']
content = '''<html><a href="http://www.not-this-one.com"></a>
<a href="http://www.somewebsite.com"></a><p>other stuff</p><p>sales</p>
</html>
'''
idx = content.find('sales')
with contextlib.closing(MyParser()) as parser:
parser.feed(content[:idx])
print(parser.last_link)
关于lxml解决方案中使用的XPath:XPath具有以下含义:
//* # Find all elements
[contains(text(),"sales")] # whose text content contains "sales"
/preceding::* # search the preceding elements
[starts-with(@href,"http")] # such that it has an href attribute that starts with "http"
[1] # select the first such <a> tag only
/@href # return the value of the href attribute