Reputation: 15
This is my html:
html = """<ul id="mostUp">
<li class="Upper"> ‹
<a href="x">Countries</a>
</li>
<ul>
<li>
<span class="country_selection">Germany</span>
</li>
<ul>
<li><a href="x">Frankfurt</a></li>
<li><a href="x">München</a></li>
</ul>
</ul>
</ul>
"""
Cause country_selection
is the only unique element which i can select, i am now trying to get the LI
's with the Cities (Frankfurt and München).
This is my code:
soup = BeautifulSoup(html)
categories = soup.find("span", class_="country_selection")
if categories.parent.parent.find("ul"):
e = categories.parent.parent.select('ul')
for subItem in e:
print(subItem)
But this gives me this result:
<ul>
<li><a href="x">Frankfurt</a></li>
<li><a href="x">München</a></li>
</ul>
So, this means, that i have the Parent UL and cannot iterate through the LIs. And i don't know why! Where is the error? How do i need to change my code to iterate through those LIs?
Upvotes: 0
Views: 256
Reputation: 548
You do not need a parent or for loop! You can get Frankfurt
and München
by this way:
soup = BeautifulSoup(html)
categories = soup.find("span", class_="country_selection")
data = categories.findNext("ul").text.strip().split()
Upvotes: 1
Reputation: 3860
If you want to get Frankfurt
and München
, you can go this way:
from bs4 import BeautifulSoup
html = """<ul id="mostUp">
<li class="Upper"> ‹
<a href="x">Countries</a>
</li>
<ul>
<li>
<span class="country_selection">Germany</span>
</li>
<ul>
<li><a href="x">Frankfurt</a></li>
<li><a href="x">München</a></li>
</ul>
</ul>
</ul>
"""
soup = BeautifulSoup(html)
categories = soup.find("span", class_="country_selection")
if categories.parent.parent.find("ul"):
e = categories.parent.parent.select('ul a')
for subItem in e:
print(subItem.text)
Prints:
Frankfurt
München
Upvotes: 1