【发布时间】:2015-05-11 02:57:50
【问题描述】:
例子:
有时 HTML 是:
<div id="1">
<div id="2">
this is the text i do NOT want
</div>
this is the text i want here
</div>
其他时候只是:
<div id="1">
this is the text i want here
</div>
我只想获取一个标签中的文本,而忽略所有其他子标签。如果我运行 .text 属性,我会得到两者。
【问题讨论】:
-
为什么不获取outer的文本并替换其直接子节点的所有文本?
-
@tuananh 如果直接子项的文本也是
<div>a b c<div>b</div>e f</div>中的父项中的字符串的一部分,您怎么知道要替换什么? -
文本是否保证跟随任何和所有内部子标签?
标签: python python-2.7 beautifulsoup