Seite 1 von 1

BeautifulSoup

Verfasst: Donnerstag 16. Februar 2023, 13:15
von xocy

Code: Alles auswählen

from bs4 import BeautifulSoup

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""



soup = BeautifulSoup(html_doc, "html.parser")

a = soup.find_next_siblings("a")
print(a)

Hallo,

ich bekomme lediglich eine leere liste zurueck. Sollten jedoch nicht die 2 anderen links auch gefunden werden oder missverstehe ich hier was?

LG

Re: BeautifulSoup

Verfasst: Donnerstag 16. Februar 2023, 13:39
von grubenfox
ja, soup ist erstmal nur Suppe und hat weder Schwestersuppen, noch Schwesterlinks.
Der erste Link

Code: Alles auswählen

first_link = soup.a
, der hat zwei Schwestern

Re: BeautifulSoup

Verfasst: Donnerstag 16. Februar 2023, 14:21
von xocy
Ich hatte da was wohl mit der abkuerzung soup("a") verwechselt, danke.

Re: BeautifulSoup

Verfasst: Freitag 17. Februar 2023, 13:57
von snafu
Um alle Links (anchor-Elemente) zu finden, würde ich einfach ``soup.find_all("a")`` nehmen...