Beautiful soup获得匹配的标记值之后的标记值



我有一个代码,我需要寻找具有值'Lead'的div标签,一旦在代码中找到该标签,它就会查找具有值'name'的直接div,然后在直接的下一个标签中提取电子邮件id,这样我的代码输出名称和电子邮件id如下

John Doe john.doe@johndoe.com

<div class="badge memberType Member long notAffiliatedContact">Lead</div>
<div class="name"><a href="/10016/John Doe">John Doe</a></div>
<div class="posted"><span class="label">Email: </span><span class="value break-word">john.doe@johndoe.com</span></div>

提前谢谢你。

您可以使用findNext()找到下一个元素。

from bs4 import BeautifulSoup
html = """
<div class="badge memberType Member long notAffiliatedContact">Lead</div>
<div class="name"><a href="/10016/John Doe">John Doe</a></div>
<div class="posted"><span class="label">Email: </span><span class="value break-word">john.doe@johndoe.com</span></div>
"""
soup = BeautifulSoup(html, 'html.parser')
divs = soup.findAll("div", {"class": "badge"})
for div in divs:
if div.text == "Lead":
name = div.findNext('div')
email = name.findNext('span').findNext('span')
print(name.text, email.text)
#John Doe john.doe@johndoe.com

相关内容

  • 没有找到相关文章

最新更新