gpt4 book ai didi

python - 使用 BeautifulSoup 选择第二个 child

转载 作者:太空狗 更新时间:2023-10-29 22:00:57 25 4
gpt4 key购买 nike

假设我有以下 HTML:

<div>
<p>this is some text</p>
<p>...and this is some other text</p>
</div>

如何使用 BeautifulSoup 检索第二段中的文本?

最佳答案

您可以使用 CSS 选择器来执行此操作:

>>> from bs4 import BeautifulSoup

>>> soup = BeautifulSoup("""<div>
.... <p>this is some text</p>
.... <p>...and this is some other text</p>
.... </div>""", "html.parser")

>>> soup.select('div > p')[1].get_text(strip=True)
'...and this is some other text'

关于python - 使用 BeautifulSoup 选择第二个 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38233838/

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