从html页面获取相关链接

2024-05-08 16:01:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我只想从html页面提取相对URL;有人建议:

find_re = re.compile(r'\bhref\s*=\s*("[^"]*"|\'[^\']*\'|[^"\'<>=\s]+)', re.IGNORECASE)

但它返回:

1/页面中的所有绝对和相对URL。在

2/url可以由""''随机量化。在


Tags: reurlhtml页面find建议compileignorecase
1条回答
网友
1楼 · 发布于 2024-05-08 16:01:29

使用the tool for the job:一个HTML parser,像^{}。在

您可以pass a function作为^{}的属性值,并检查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

^{pr2}$

两种解决方案都打印:

[<a href="test2">test2</a>, 
 <a href="here/we/go">test4</a>]

相关问题 更多 >

    热门问题