在美丽的汤中选择第二个孩子

use*_*884 7 python beautifulsoup web-scraping

让我们说:

<div>
    <p>this is some text</p>
    <p>...and this is some other text</p>
</div>
Run Code Online (Sandbox Code Playgroud)

如何从beautifulsoup的第二段中检索文本?

sty*_*ane 14

您可以使用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'
Run Code Online (Sandbox Code Playgroud)

  • @ hashcode55怎么样:`soup.select_one('div> p:nth-​​of-type(2)').get_text(strip = True)`? (5认同)

Pad*_*ham 11

你可以使用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)
Run Code Online (Sandbox Code Playgroud)