如何获得使用BeautifulSoup嵌套的标签内容 [英] How to get contents of nested tag using BeautifulSoup
本文介绍了如何获得使用BeautifulSoup嵌套的标签内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何使用BeautifullSoup收盘span标签之前到多少?
<跨度类=计数>
< I类=图标的用户>< I&GT /;
30.5K< / SPAN>
我可以使用:
= USERCOUNT soup.findAll('跨',{'类':'数'})
而不是:
= USERCOUNT soup.findAll('我',{'类':'数'})
解决方案
你后的文字是在&LT文本节点;在
<$; I&GT C $ C>&LT;跨度&GT; :
进口BS4汤= bs4.BeautifulSoup('''
&LT;跨度类=计数&GT;
&LT; I类=图标的用户&GT;&LT; I&GT /;
30.5K&LT; / SPAN&GT;
''')= USERCOUNT soup.find('跨',类_ =计数)。找到(I)。next.strip()
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'})
解决方案
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()
这篇关于如何获得使用BeautifulSoup嵌套的标签内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文