Selecting nested element with beautiful soup

こ雲淡風輕ζ 提交于 2020-01-01 19:29:11

问题


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


回答1:


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

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



回答2:


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


来源:https://stackoverflow.com/questions/25146415/selecting-nested-element-with-beautiful-soup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!