我一直在尝试制作一个Python脚本,该脚本实际上是从特定元素块中提取文本,但必须排除嵌套兄弟姐妹中的某些文本。
这是我要抓取的HTML部分:
<div class="article_body">
<div id="articleBodyContents">
Stack Overflow
<br/>
Is Love
<br/>
<a href="https://example_site1.com" target="_blank">Ad</a>
<br/>
<a href="https://example_site2.com" target="_blank">Ad2</a>
</div>
</div>
到目前为止,我已经取得了进步:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, "html.parser")
divs = soup.findAll('div', {'id':'articleBodyContents'})
for ops in divs:
print(ops.text.replace('\n', '').strip())
但是此打印输出:
Stack Overflow
Is love
Ad
Ad2
我只想要:
Stack Overflow
Is love
答案 0 :(得分:0)
您快到了。您需要NavigableString
的帮助才能实现此目的。只需捕获前一个父级,然后遍历它,检查字符串是否为NavigableString
的实例。这是您的代码:
from bs4 import BeautifulSoup, NavigableString
html = """
<div class="article_body">
<div id="articleBodyContents">
Stack Overflow
<br/>
Is love
<br/>
<a href="https://example_site1.com" target="_blank">Ad</a>
<br/>
<a href="https://example_site2.com" target="_blank">Ad2</a>
</div>
</div>
"""
soup = BeautifulSoup(html, "html.parser")
divs = soup.find('div', {'class':'article_body'})
ops = [element for element in divs.div if isinstance(element, NavigableString)]
for op in ops:
print(op.strip().replace('\n', ''))
输出:
Stack Overflow
Is love