[英]lxml - ignore <br> tag in html
I wrote a tiny html-parser in Python using lxml. 我使用lxml在Python中编写了一个小的html解析器。 It's very useful, but I have a problem.
这很有用,但是我有一个问题。
I have the following code: 我有以下代码:
tags = doc.xpath('//table//tr/td[@align="right"]/b')
for tag in tags:
print(x.text.strip())
It works fine. 工作正常。 But if there is a
<br>
tag inside a <b>
element, like this: 但是,如果
<b>
元素内有一个<br>
标签,如下所示:
<b> first-half <br>
second-half </b>
this code will only print first-half
into the <b>
tag. 此代码仅将
first-half
打印到<b>
标记中。
How can I get all of text in <b>
even if there is a <br>
tag? 即使有
<br>
标签,如何获取<b>
所有文本?
Thanks. 谢谢。
Use text_content()
to extract all of the non-markup text within a tag. 使用
text_content()
提取标签中的所有非标记文本。 Replace x.text
with x.text_content()
. 将
x.text
替换为x.text_content()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.