简体   繁体   English

选择嵌套元素与美丽的汤

[英]Selecting nested element with beautiful soup

I have the following html: 我有以下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: Mauro的回答可能更符合您的要求,但这是另一种方法,以及我如何考虑获取内部div文本:

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. BeautifouSoup select检索列表。 You must specify the index. 您必须指定索引。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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