通过网络抓取提取电子邮件python不起作用

2024-09-29 19:01:17 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能帮我写一段代码,通过BeautifulSoup从下面的HTML中提取电子邮件吗?我试过了

  1. select方法
  2. find方法
  3. 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>

enter image description here

我正在尝试,但给出了一个空列表,[]

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

Tags: 方法dividstyleemailtopbarfind
1条回答
网友
1楼 · 发布于 2024-09-29 19:01:17

.select()方法只接受CSS选择器(标记名、ID、类和其他CSS选择器语法),而不是整个CSS声明(没有style属性的内容);您可以搜索:

soup.select('div#top-bar-branding-extra span')

因为您无法在此处使用CSS搜索style属性。然后,您可以进一步筛选匹配的元素:

for span in soup.select('div#top-bar-branding-extra span'):
    if span.get('style') == 'color:#cf3023;':
        email = span.text
        break

或者将其设为生成器表达式,默认为None

email = next((s.text for s in soup.select('div#top-bar-branding-extra span')
              if s.get('style') == 'color:#cf3023;'), None)

但您需要查看实际的页面源(而不是浏览器DOM表示),以查看它是否与实际的属性文本足够匹配

如果您发布的HTML源代码是准确的,则上述操作有效:

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('''\
... <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>
... ''')
>>> for span in soup.select('div#top-bar-branding-extra span'):
...     if span.get('style') == 'color:#cf3023;':
...         email = span.text
...         break
... 
>>> email
u' info@home4all.ae'
>>> email = next((s.text for s in soup.select('div#top-bar-branding-extra span')
...               if s.get('style') == 'color:#cf3023;'), None)
>>> email
u' info@home4all.ae'

请注意,这要求从URL加载的实际源包含此结构。从HTML判断,您正在尝试从页面上的Google广告加载电子邮件,该广告总是通过JavaScript加载,并且不是原始源的一部分

您必须分析Google是如何加载广告并在Python中复制广告的,或者使用完整的web客户端(如ghost或selenium驱动的浏览器)来执行Javascript,检索结果DOM,然后解析而不是

相关问题 更多 >

    热门问题