2024-05-08 16:01:29 发布
网友
我只想从html页面提取相对URL;有人建议:
find_re = re.compile(r'\bhref\s*=\s*("[^"]*"|\'[^\']*\'|[^"\'<>=\s]+)', re.IGNORECASE)
但它返回:
1/页面中的所有绝对和相对URL。在
2/url可以由""或''随机量化。在
""
''
使用the tool for the job:一个HTML parser,像^{}。在
HTML parser
您可以pass a function作为^{}的属性值,并检查href是否以http开头:
href
http
from bs4 import BeautifulSoup data = """ <div> <a href="http://google.com">test1</a> <a href="test2">test2</a> <a href="http://amazon.com">test3</a> <a href="here/we/go">test4</a> </div> """ soup = BeautifulSoup(data) print soup.find_all('a', href=lambda x: not x.startswith('http'))
或者,使用^{}和checking for network location part:
两种解决方案都打印:
[<a href="test2">test2</a>, <a href="here/we/go">test4</a>]
使用the tool for the job:一个} 。在
HTML parser
,像^{您可以pass a function作为^{} 的属性值,并检查
href
是否以http
开头:或者,使用^{} 和checking for network location part:
^{pr2}$两种解决方案都打印:
相关问题 更多 >
编程相关推荐