如何从标签中获取文本,但忽略其他子标签 [英] how to get text from within a tag, but ignore other child tags
本文介绍了如何从标签中获取文本,但忽略其他子标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在煮漂亮的汤. 我有一个html字符串:
I am working with beautiful soup. I have a html string:
<div><b>ignore this</b>get this</div>
如何在忽略"忽略此"
谢谢
推荐答案
您可以获取div
文本,而不必递归检索子文本:
You can get the div
text just not recursively retrieving the children texts:
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('<div><b>ignore this</b>get this</div>')
>>> soup.div.find(text=True, recursive=False)
u'get this'
这与文本相对于孩子的位置无关:
This works independently of the position of the text with respect of the children:
>>> soup = BeautifulSoup('<div>get this<b>ignore this</b></div>')
>>> soup.div.find(text=True, recursive=False)
u'get this'
这篇关于如何从标签中获取文本,但忽略其他子标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文