是否可以只获取特定的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
我是按字符串逻辑做的。有没有直接使用美偶的方法?
可以匹配多个方面,包括对属性值使用正则表达式:
哪个匹配(例如):
所以任何具有
href
属性的<a>
标记,其值以字符串http://www.iwashere.com/
开头。您可以循环查看结果,并只选择
href
属性:要匹配所有相对路径,请使用一个否定的前瞻性断言,测试该值是否以schem(例如
http:
或mailto:
)或双斜杠(//hostname/path
)开头,而不是以相对路径开头:如果使用BeautifulSoup 4.0.0或更高版本:
相关问题 更多 >
编程相关推荐