问题
How can I use BeautifullSoup to get to the number before the closing span tag?
<span class="count">
<i class="icon-user"></i>
30.5K </span>
I can use:
usercount=soup.findAll('span',{'class':'count'})
but not:
usercount=soup.findAll('i',{'class':'count'})
回答1:
The text you're after is the text node after the <i> in the <span>:
import bs4
soup = bs4.BeautifulSoup('''
<span class="count">
<i class="icon-user"></i>
30.5K </span>
''')
usercount = soup.find('span', class_='count').find('i').next.strip()
来源:https://stackoverflow.com/questions/20982406/how-to-get-contents-of-nested-tag-using-beautifulsoup