在带有美丽汤的特定标签后获取文本 [英] get text after specific tag with beautiful soup
本文介绍了在带有美丽汤的特定标签后获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有类似的文字
page.content = "<body><b>Title:</b> Test title</body>"
我可以通过
soup = BeautifulSoup(page.content)
record_el = soup('body')[0]
b_el = record_el.find('b',text='Title:')
但是如何获取b标记之后的文本?我想通过引用该元素而不是body元素来获取包含"Title:"的元素之后的文本.
but how can I get the text after the b tag? I would like to get the text after the element containing "Title:" by referring to that element, and not the body element.
推荐答案
Referring to the docs you might want to use the next_sibling
of your b_el
:
b_el.next_sibling # contains " Test title"
在此上下文中,同级"是下一个节点,而不是下一个元素/标签.元素的下一个节点是文本节点,因此您可以获取想要的文本.
"Sibling" in this context is the next node, not the next element/tag. Your element's next node is a text node, so you get the text you want.
这篇关于在带有美丽汤的特定标签后获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文