Python美化组提取特定url

2024-06-28 15:40:31 发布

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

是否可以只获取特定的url?

比如:

<a href="http://www.iwashere.com/washere.html">next</a>
<span class="class">...</span>
<a href="http://www.heelo.com/hello.html">next</a>
<span class="class">...</span>
<a href="http://www.iwashere.com/wasnot.html">next</a>
<span class="class">...</span>

输出应仅为来自http://www.iwashere.com/的URL

例如,输出URL:

http://www.iwashere.com/washere.html
http://www.iwashere.com/wasnot.html

我是按字符串逻辑做的。有没有直接使用美偶的方法?


Tags: comhttpurlhellohtmlwwwclassnext
2条回答

可以匹配多个方面,包括对属性值使用正则表达式:

import re
soup.find_all('a', href=re.compile('http://www\.iwashere\.com/'))

哪个匹配(例如):

[<a href="http://www.iwashere.com/washere.html">next</a>, <a href="http://www.iwashere.com/wasnot.html">next</a>]

所以任何具有href属性的<a>标记,其值以字符串http://www.iwashere.com/开头。

您可以循环查看结果,并只选择href属性:

>>> for elem in soup.find_all('a', href=re.compile('http://www\.iwashere\.com/')):
...     print elem['href']
... 
http://www.iwashere.com/washere.html
http://www.iwashere.com/wasnot.html

要匹配所有相对路径,请使用一个否定的前瞻性断言,测试该值是否以schem(例如http:mailto:)或双斜杠(//hostname/path)开头,而不是以相对路径开头:

soup.find_all('a', href=re.compile(r'^(?!(?:[a-zA-Z][a-zA-Z0-9+.-]*:|//))'))

如果使用BeautifulSoup 4.0.0或更高版本:

soup.select('a[href^="http://www.iwashere.com/"]')

相关问题 更多 >