Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selecting second child in beautiful soup

Let's say I have the following HTML:

<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 using BeautifulSoup?

like image 257
user3885884 Avatar asked Jul 06 '16 21:07

user3885884


2 Answers

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'
like image 135
styvane Avatar answered Oct 19 '22 05:10

styvane


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)
like image 37
Padraic Cunningham Avatar answered Oct 19 '22 06:10

Padraic Cunningham