gpt4 book ai didi

python - 使用 bs4 查找包含文本的 html 标签 (h2)

转载 作者:太空宇宙 更新时间:2023-11-04 10:12:28 24 4
gpt4 key购买 nike

对于这部分html代码:

html3= """<a name="definition"> </a>
<h2><span class="sectioncount">3.342.2323</span> Content Logical Definition <a title="link to here" class="self-link" href="valueset-investigation"><img src="ta.png"/></a></h2>
<hr/>
<div><p from the following </p><ul><li>Include these codes as defined in http://snomed.info/sct<table><tr><td><b>Code</b></td><td><b>Display</b></td></tr><tr><td>34353553</td><td>Examination / signs</td><td/></tr><tr><td>35453453453</td><td>History/symptoms</td><td/></tr></table></li></ul></div>
<p> </p>"""

我将使用 beautifulsoup 查找其文本等于“Content Logical Definition”和下一个 sibling 的 h2。但是beautifulsoup找不到h2。以下是我的代码:

soup = BeautifulSoup(html3, "lxml")
f= soup.find("h2", text = "Content Logical Definition").nextsibilings

这是一个错误:

AttributeError: 'NoneType' object has no attribute 'nextsibilings'

文中有几个“h2”,但唯一使这个h2独一无二的字符是“Content Logical Definition”。找到这个h2之后,我要从表中提取数据并在其下列出。

最佳答案

主要问题是您定位 h2 元素以从中查找 sibling 的方式。我会使用 function而是检查 Content Logical Definition 是否在文本中:

soup.find(lambda elm: elm.name == "h2" and "Content Logical Definition" in elm.text)

此外,要获得下一个 sibling ,您应该使用 .next_siblings而不是 nextsibilings

演示:

>>> from bs4 import BeautifulSoup
>>> html3= """<a name="definition"> </a>
... <h2><span class="sectioncount">3.342.2323</span> Content Logical Definition <a title="link to here" class="self-link" href="valueset-investigation"><img src="ta.png"/></a></h2>
... <hr/>
... <div><p from the following </p><ul><li>Include these codes as defined in http://snomed.info/sct<table><tr><td><b>Code</b></td><td><b>Display</b></td></tr><tr><td>34353553</td><td>Examination / signs</td><td/></tr><tr><td>35453453453</td><td>History/symptoms</td><td/></tr></table></li></ul></div>
... <p> </p>"""
>>> soup = BeautifulSoup(html3, "lxml")
>>> h2 = soup.find(lambda elm: elm.name == "h2" and "Content Logical Definition" in elm.text)
>>> for sibling in h2.next_siblings:
... print(sibling)
...
<hr/>
<div><p following="" from="" the=""></p><ul><li>Include these codes as defined in http://snomed.info/sct<table><tr><td><b>Code</b></td><td><b>Display</b></td></tr><tr><td>34353553</td><td>Examination / signs</td><td></td></tr><tr><td>35453453453</td><td>History/symptoms</td><td></td></tr></table></li></ul></div>
<p> </p>

不过,现在知道您正在处理的真实 HTML 以及它有多困惑,我认为您应该迭代 sibling ,在下一个 h2 处中断,或者如果您找到 table 之前。实际执行:

import requests
from bs4 import BeautifulSoup

urls = [
'https://www.hl7.org/fhir/valueset-activity-reason.html',
'https://www.hl7.org/fhir/valueset-age-units.html'
]

for url in urls:
r = requests.get(url)
soup = BeautifulSoup(r.content, 'lxml')

h2 = soup.find(lambda elm: elm.name == "h2" and "Content Logical Definition" in elm.text)
table = None
for sibling in h2.find_next_siblings():
if sibling.name == "table":
table = sibling
break
if sibling.name == "h2":
break
print(table)

关于python - 使用 bs4 查找包含文本的 html 标签 (h2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37511490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com