dropWizard
dropWizard

Reputation: 3538

Navigating DOM in BeautifulSoup

I'm currently able to find certain elements using the findAll function. Is there a way to navigate to their child?

The code I have is:

data = soup.findAll(id="profile-experience")
print data[0].get_text()

And it returns a block of text (for example, some of the text isn't spaced out properly)

The DOM looks something like this

<div id="profile-experience>
  <div class="module-body>
    <li class="position">
    <li class="position">
    <li class="position">

If I just do a findAll on class="position I get way too much crap back. Is there a way using BeautifulSoup to just find the elements that are <li class="position"> that are nested underneath <div id="profile-experience">

I want to do something like this:

data = soup.findAll('li',attrs={'class':'position'}) 

(Where I'm only getting the nested data)

d in data:
  print d.get_text()

Upvotes: 1

Views: 4050

Answers (1)

alecxe
alecxe

Reputation: 474041

Sure, you can "chain" the find* calls:

profile_experience = soup.find(id="profile-experience")

for li in profile_experience.find_all("li", class_="position"):
    print(li.get_text())

Or, you can solve it in one go with a CSS selector:

for li in soup.select("#profile-experience li.position"):
    print(li.get_text())

Upvotes: 2

Related Questions