Python BS4无法正确提取数据

时间:2020-05-27 20:43:37

标签: python beautifulsoup

所以我有这个源代码

<div class="field-fluid col-xs-12 col-sm-12 col-md-12 col-lg-6">
    <div class="label-fluid">Email Address</div>
    <div class="data-fluid rev-field" aria-data="rei-0">maldapalmer<span class="hd-form-field">ajk89;fjioasjdfwjepu90f30 v09u30r nv8704rhnv987rjl3409u0asu[amav084-8235 087307304u0[9fd0]] asf74 john 9@#83r8cva sarah sj4t8g@!$%#7h v7hgv 398#$&amp;&amp;^@7y9</span>@gmail<span class="hd-form-field">ajk89;fjioasjdfwjepu90f30 v09u30r nv8704rhnv987rjl3409u0asu[amav084-8235 087307304u0[9fd0]] asf74 john 9@#83r8cva sarah sj4t8g@!$%#7h v7hgv 398#$&amp;&amp;^@7y9</span>.com</div>
</div>

我似乎做得很好,但是我无法提取位于主div元素内第二个div中的电子邮件地址。这是我的代码:

fields = []
for row in rows:
    fields.append(row.find_all('div', recursive = False))
email = fields[0][0].find(class_ = "data-fluid rev-field").text

以下是主要div中的元素所在的行。欢迎提出任何建议,也希望我对这个问题的解释足够好。

我得到的问题是字符串显示为空''。谢谢!

1 个答案:

答案 0 :(得分:3)

您可以使用以下代码提取电子邮件:

from bs4 import *
from requests import get
response = get('http://127.0.0.1/bs.html') # Replce 'http://127.0.0.1/bs.html' with your URL
sp = BeautifulSoup(response.text, 'html.parser')
email = sp.find('div', class_= "data-fluid rev-field").text
spn = sp.find('span', class_= "hd-form-field").text
email = email.replace(spn,"")
print(email)

输出:

maldapalmer@gmail.com