微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python:如何使用BeautifulSoup从HTML页面中提取URL?

我有一个包含多个div的HTML页面

aimed to be the representative of one of the six accused in the December 16 gangrape case who has sought shifting of t... angrape-case-two-lawyers-claim-to-be-engaged-by-accused/article4332680.ece"> distribution companies – the Anil Ambani-owned BRPL and BYPL and the Tatas-owned Tata Powe... discoms-demand-yet-another-hike-in-charges/article4331482.ece"> discoms-demand-yet-another-hike-in-charges/article4331482.ece#comments">

我需要得到< a href =>具有类article-additional-info的所有div的值
我是BeautifulSoup的新手

所以我需要网址

"http://www.thehindu.com/news/national/gangrape-case-two-lawyers-claim-to-be-engaged-by-accused/article4332680.ece"
"http://www.thehindu.com/news/cities/Delhi/power-discoms-demand-yet-another-hike-in-charges/article4331482.ece"

实现这一目标的最佳方法是什么?

最佳答案
根据您的标准,它返回三个URL(而不是两个) – 您想要过滤掉第三个吗?

基本思想是迭代HTML,只抽取你的类中的那些元素,然后迭代该类中的所有链接,拉出实际的链接

In [1]: from bs4 import BeautifulSoup

In [2]: html = # your HTML

In [3]: soup = BeautifulSoup(html)

In [4]: for item in soup.find_all(attrs={'class': 'article-additional-info'}):
   ...:     for link in item.find_all('a'):
   ...:         print link.get('href')
   ...:         
http://www.thehindu.com/news/national/gangrape-case-two-lawyers-claim-to-be-engaged-by-accused/article4332680.ece
http://www.thehindu.com/news/cities/Delhi/power-discoms-demand-yet-another-hike-in-charges/article4331482.ece
http://www.thehindu.com/news/cities/Delhi/power-discoms-demand-yet-another-hike-in-charges/article4331482.ece#comments

这会将您的搜索范围限制为仅包含article-additional-info类标记的元素,并在其中查找所有锚点(a)标记获取其相应的href链接.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐