user1592380
user1592380

Reputation: 36215

Selecting nested element with beautiful soup

I have the following html:

<div class="leftColumn">
  <div>
     <div class="static">
     text1
     <br>
     text2
     <br>
     (222) 123 - 4567
     <br>
     <div class="summary">

How can I select just the text lines using beautiful soup.

I've tried a variety of things like:

soup.select('.leftColumn div').text

but so far no dice

Upvotes: 1

Views: 3585

Answers (2)

Mauro Baraldi
Mauro Baraldi

Reputation: 6550

BeautifouSoup select retrives a list. You must specify the index.

soup.select('.leftColumn div')[0].text.split()

Upvotes: 1

heinst
heinst

Reputation: 8786

Mauro's answer is probably more what you wanted, but this is another way to do it and how I thought about getting the inner div text:

from bs4 import BeautifulSoup
html = '''<div class="leftColumn">
  <div>
     <div class="static">
     text1
     <br>
     text2
     <br>
     (222) 123 - 4567
     <br>
     <div class="summary">
     '''
bs = BeautifulSoup(html)
for div in bs.findAll('div', attrs={'class': 'leftColumn'}):
    print div.findNext('div').findNext('div').text

Upvotes: 4

Related Questions