简体   繁体   中英

selecting second child in beautiful soup

Let say a have:

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

How can I retrieve the text from the second paragraph in beautifulsoup?

You can use a CSS selector to do this:

>>> 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'

You can use 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)
secondp = [div.find('p') for div in soup.find('div')]

In : secondp[1].text

Out : Your text

Or you can use the findChildren directly -

div_ = soup.find('div').findChildren()
for i, child in enumerate(div_):
    if i == 1:
         print child.text

You could solve this with gazpacho :

from gazpacho import Soup

html = """\
<div>
    <p>this is some text</p>
    <p>...and this is some other text</p>
</div>
"""

soup = Soup(html)
soup.find('p')[1].text

Which would output:

'...and this is some other text'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM