有人能帮我浏览一个html树和美丽的汤吗?
我试图通过html输出进行解析,在收集每个值之后,使用python/django将其插入到名为Tld
的表中
<div class="rc" data-hveid="53">
<h3 class="r">
<a href="https://billing.anapp.com/" onmousedown="return rwt(this,'','','','2','AFQjCNGqpb38ftdxRdYvKwOsUv5EOJAlpQ','m3fly0i1VLOK9NJkV55hAQ','0CDYQFjAB','','',event)">Billing: Portal Home</a>
</h3>
只解析<a>
的href
属性的值,所以只有这一部分:
https://billing.anapp.com/
第页,共页:
<a href="https://billing.anapp.com/" onmousedown="return rwt(this,'','','','2','AFQjCNGqpb38ftdxRdYvKwOsUv5EOJAlpQ','m3fly0i1VLOK9NJkV55hAQ','0CDYQFjAB','','',event)">Billing: Portal Home</a>
我现在有:
for url in urls:
mb.open(url)
beautifulSoupObj = BeautifulSoup(mb.response().read())
beautifulSoupObj.find_all('h3',attrs={'class': 'r'})
问题是上面的find_all
,还不够到<a>
元素。
任何帮助都非常感谢。 谢谢您。
印刷品:
h3.r a
是一个css selector您可以使用css选择器(我更喜欢)、xpath或find-in元素。选择器
h3.r a
将查找具有类r
的所有h3
,并从其中获取a
元素。它可能是一个更复杂的例子,比如#an_id table tr.the_tr_class td.the_td_class
它会在给定的类中找到一个属于tr的给定td的id,当然它也在一个表中。这也会给你同样的结果。
find_all
返回bs4.element.Tag
的列表,find_all
有一个递归字段不确定是否可以在一行中完成,我个人更喜欢css选择器,因为它简单而干净。相关问题 更多 >
编程相关推荐