有人能帮我写一段代码,通过BeautifulSoup从下面的HTML中提取电子邮件吗?我试过了
select
方法find
方法find_all
方法HTML:
<div id="google_ads_div_990x50-Top_Bar-Classified_Detail_ad_wrapper">
<div id="google_ads_div_990x50-Top_Bar-Classified_Detail_ad_container" style="display:inline-block;">
<div id="top-bar-branding">
<div id="top-bar-branding-logo" style="margin-right:20px margin-left:6px">
<div id="top-bar-branding-text" style="color:#000; font-size:14px; font-weight:bold; width:450px; text-align:center">As we promised</div>
<div id="top-bar-branding-extra" style="color:#000; font-size:14px; font-weight:bold;">
<span style="color:#444; font-weight:normal;">Telephone </span>
04 451 3111
<span style="color:#444; font-weight:normal;">or email </span>
<span style="color:#cf3023;"> info@home4all.ae</span>
</div>
</div>
</div>
</div>
</div>
</div>
我正在尝试,但给出了一个空列表,[]
:
email=soup.select("div #top-bar-branding-extra color:#cf3023;")
print email
这也不起作用:
div = soup.find("div", {"id":"top-bar-branding-extra"})
span = div.find("span", {"style":"color:#cf3023;"})
print span.string
.select()
方法只接受CSS选择器(标记名、ID、类和其他CSS选择器语法),而不是整个CSS声明(没有style
属性的内容);您可以搜索:因为您无法在此处使用CSS搜索
style
属性。然后,您可以进一步筛选匹配的元素:或者将其设为生成器表达式,默认为
None
:但您需要查看实际的页面源(而不是浏览器DOM表示),以查看它是否与实际的属性文本足够匹配
如果您发布的HTML源代码是准确的,则上述操作有效:
请注意,这要求从URL加载的实际源包含此结构。从HTML判断,您正在尝试从页面上的Google广告加载电子邮件,该广告总是通过JavaScript加载,并且不是原始源的一部分
您必须分析Google是如何加载广告并在Python中复制广告的,或者使用完整的web客户端(如ghost或selenium驱动的浏览器)来执行Javascript,检索结果DOM,然后解析而不是
相关问题 更多 >
编程相关推荐