How to get contents of nested tag using BeautifulSoup

半城伤御伤魂 提交于 2019-12-25 01:53:49

问题


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

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