假设我有以下 HTML:
<div>
<p>this is some text</p>
<p>...and this is some other text</p>
</div>
如何使用 BeautifulSoup 从第二段中检索文本?
原文由 user3885884 发布,翻译遵循 CC BY-SA 4.0 许可协议
假设我有以下 HTML:
<div>
<p>this is some text</p>
<p>...and this is some other text</p>
</div>
如何使用 BeautifulSoup 从第二段中检索文本?
原文由 user3885884 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以使用 nth-of-type :
h = """<div>
<p>this is some text</p>
<p>...and this is some other text</p>
</div>"""
soup = BeautifulSoup(h)
print(soup.select_one("div p:nth-of-type(2)").text)
原文由 Padraic Cunningham 发布,翻译遵循 CC BY-SA 3.0 许可协议
2 回答5.2k 阅读✓ 已解决
2 回答1.1k 阅读✓ 已解决
4 回答1.4k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
2 回答884 阅读✓ 已解决
1 回答1.8k 阅读✓ 已解决
您可以使用 CSS 选择器来执行此操作: