简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM