最近我开始抓取多个页面,但是页面的结构很难刮。它有很多“第n类”元素,它们没有针对每个自身的类。但他们的父母同班。我和Beautiulsoup一起工作,直到我看到这个糟糕的代码。。。在
<div class="detail-50">
<div class="detail-panel-wrap">
<h3>Contact details</h3>
Website: <a href="http://www.somewebsitefrompage.com">http://www.somewebsitefrompage.com</a><br />Email: <a href="mailto:somemailfrompage.com">somemailfrompage.com</a><br />Tel: 11111111 111
</div>
</div>
目前看来还可以,但我想把网站,电子邮件和电话分开。我尝试了很多方法,比如
^{pr2}$但不起作用。。现在,当其他元素具有与联系人详细信息相同的类时,出现了一个巨大的问题:
<div class="detail-50">
<div class="detail-panel-wrap">
<h3>Public address</h3>
Mr Martin Austin, Some street, Some city, some ZIP
</div>
</div>
这是地址,我也需要刮。还有很多其他的“div”名字,比如这两个。有人有解决办法吗?如果有人不明白,我可以解释得更好,抱歉解释不好。。在
更新
通过一些选择器软件,我发现了它应该是什么样子,但是用python编写它很困难。。以下是如何从第页找到电话:
div#ContentPlaceHolderDefault_cp_content_ctl00_CharityDetails_4_TabContainer1_tpOverview_plContact.detail-panel div.detail-50:nth-of-type(1) div.detail-panel-wrap
这个是地址
div#ContentPlaceHolderDefault_cp_content_ctl00_CharityDetails_4_TabContainer1_tpOverview_plContact.detail-panel div.detail-50:nth-of-type(2) div.detail-panel-wrap
这个是网站的
div.detail-50 a:nth-of-type(1)
这个是联系邮件
div.detail-panel-wrap a:nth-of-type(2)
注:
ContentPlaceHolderDefault_cp_content_ctl00_CharityDetails_4_TabContainer1_tpOverview_plContact
父div类位于所有这些类的顶部。在
有人知道如何用BS4Python编写这些代码吗?在
如果有多个divdetail panel wrap,则可以使用h3文本来获取所需的div:
如果我们在一个样本上运行,你可以看到我们得到两个div:
^{pr2}$可能还有其他方法,但是如果没有看到完整的html结构,就不可能知道。在
对于您的编辑,您只需使用选择器和select-one:
但不能保证仅仅因为选择器在chrome工具中工作。。他们会在你找到的源头上工作。在
相关问题 更多 >
编程相关推荐