问题
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