sagen wir mal eine haben:Auswahl zweites Kind in schöne Suppe
<div>
<p>this is some text</p>
<p>...and this is some other text</p>
</div>
Wie kann ich den Text aus dem zweiten Absatz in beautifulsoup abrufen?
sagen wir mal eine haben:Auswahl zweites Kind in schöne Suppe
<div>
<p>this is some text</p>
<p>...and this is some other text</p>
</div>
Wie kann ich den Text aus dem zweiten Absatz in beautifulsoup abrufen?
Sie einen CSS-Selektor, dies zu tun verwenden können:
In [57]: from bs4 import BeautifulSoup
In [58]: soup = BeautifulSoup("""<div>
....: <p>this is some text</p>
....: <p>...and this is some other text</p>
....: </div>""", "html.parser")
In [59]: soup.select('div > p')[1].get_text(strip=True)
Out[59]: '...and this is some other text'
secondp = [div.find('p') for div in soup.find('div')]
In : secondp[1].text
Out : Your text
Oder Sie können die Verwendung findChildren
direkt -
div_ = soup.find('div').findChildren()
for i, child in enumerate(div_):
if i == 1:
print child.text
Sie können n-of-Typ:
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)
Sie versprach, bei 100k zu stoppen! – alecxe
@alecxe, ich habe versehentlich eine Registerkarte geöffnet und ich sah die beautifulsoup-Tag, das ist definitiv es, nur den Browser schließen jetzt schwöre ich;) –
Kann nicht pythonischer sein! – hashcode55
@ hashcode55 Wie wäre es mit: 'supp.select_one ('div> p: n-type (2)'). Get_text (strip = True)'? – alecxe
danke. das ist genau das, was ich gesucht habe – user3885884