beautifulsoup-在br标签的任一侧获取文本 [英] beautifulsoup - Fetching text either side of a br tag
本文介绍了beautifulsoup-在br标签的任一侧获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不幸的是,我陷入了以下问题:
I have unfortunately become stuck with the following problem:
<a href="someurl">
"TEXT ONE"
<br>
"TEXT TWO"
</a>
我需要分别输入文本一和文本二.通过使用"text = container.a.text"
,我只能以文本一文本二"
的形式获得它们,而容器是a的父项标签.我尝试了很多方法,但都没有成功.我无法正确使用 br
标记.
谢谢您的帮助.
I need text one and text two separately.
I can only obtain them as a whole providing "TEXT ONE TEXT TWO"
by using "text = container.a.text"
, with the container being a parent of the a tags. I have tried as many ways as I could find with no success. I can't manage to use br
tag properly.
Thank you for any help.
推荐答案
找到
br .previousSibiling 和 .nextSibling
属性>标签:
You could use .previousSibiling
and .nextSibling
attributes after finding the br
tag:
>>> container.a.find("br").previousSibling
' \n"TEXT ONE"\n '
>>> container.a.find("br").nextSibling
'\n "TEXT TWO"\n '
这篇关于beautifulsoup-在br标签的任一侧获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文