HTML 用 lxml 解析,python,.tail 被 <br> 标签分解
HTML parsing with lxml, python, .tail being broken up by <br> tags
我有一个网站正在尝试抓取(虽然不是很了解 html),但我已经阅读了大量内容并取得了一些进展。这是一个混乱的网站,但重要的部分如下所示:
<h1>
<b>DESCRIPTOR1: </b>
" important content "
<br>
<b>DESCRIPTOr2: </b>
" important content"
<hr>
</h1>
<b>Title1</b>
" A lot of important text"
<br>
<br>
<b>Title2</b>
"A lot of important text"
<br>
<br>
<b>Title3</b>
<br>
"1. List of text pertaining to Title3 "
<br>
"2. List of items for Title 3"
<br>
"3. the number of listed items is variable for every page"
<br>
"4. Sometimes no list at all"
<br>
<br>
<b> Next Title: </b>
....and so on
现在我可以非常接近我想要的最终结果,除了当我到达标题 3 并且在标题 3 的内容之前有一个 <br>
。这就是我的方式正在接近它:
import lxml.html
htmltree = lxml.html.parse('sample.html')
items = htmltree.xpath('//*[@id="sampletext"]/b')
for node in items:
print (node.text.strip())
print node.tail
现在我的 2 个问题是 (1):我无法从 .tail's
中去除空格和 (2):我得到 "None" 返回给 Title3,因为没有 [=17] =] 在下一个元素之前 <br>
。理想情况下,在到达下一个 Identifier-Tag(在本例中为 <b>
之前,我将能够添加不在元素标记之间的任何文本。希望这是有道理的。有什么指点吗?
您可以尝试使用以下 XPath 表达式:
for item in items:
result = item.xpath('following-sibling::text()[normalize-space()][preceding-sibling::b[1] = $b]', b=item)
print [r.strip() for r in result]
针对有问题的 HTML 片段进行测试时的输出:
['" A lot of important text"']
['"A lot of important text"']
['"1. List of text pertaining to Title3 "', '"2. List of items for Title 3"', '"3. the number of listed items is variable for every page"', '"4. Sometimes no list at all"']
[]
关于XPath的简要说明:
following-sibling::text()[normalize-space()]
: 查找非空的、跟随兄弟的文本节点...
[preceding-sibling::b[1] = $b]
:...最近的前同级 b
元素等于 $b
。 $b
是一个 XPath 参数,在上面的代码中被替换为当前的 item
。这由 xpath()
方法的第二个参数指示 (b=item
)
我有一个网站正在尝试抓取(虽然不是很了解 html),但我已经阅读了大量内容并取得了一些进展。这是一个混乱的网站,但重要的部分如下所示:
<h1>
<b>DESCRIPTOR1: </b>
" important content "
<br>
<b>DESCRIPTOr2: </b>
" important content"
<hr>
</h1>
<b>Title1</b>
" A lot of important text"
<br>
<br>
<b>Title2</b>
"A lot of important text"
<br>
<br>
<b>Title3</b>
<br>
"1. List of text pertaining to Title3 "
<br>
"2. List of items for Title 3"
<br>
"3. the number of listed items is variable for every page"
<br>
"4. Sometimes no list at all"
<br>
<br>
<b> Next Title: </b>
....and so on
现在我可以非常接近我想要的最终结果,除了当我到达标题 3 并且在标题 3 的内容之前有一个 <br>
。这就是我的方式正在接近它:
import lxml.html
htmltree = lxml.html.parse('sample.html')
items = htmltree.xpath('//*[@id="sampletext"]/b')
for node in items:
print (node.text.strip())
print node.tail
现在我的 2 个问题是 (1):我无法从 .tail's
中去除空格和 (2):我得到 "None" 返回给 Title3,因为没有 [=17] =] 在下一个元素之前 <br>
。理想情况下,在到达下一个 Identifier-Tag(在本例中为 <b>
之前,我将能够添加不在元素标记之间的任何文本。希望这是有道理的。有什么指点吗?
您可以尝试使用以下 XPath 表达式:
for item in items:
result = item.xpath('following-sibling::text()[normalize-space()][preceding-sibling::b[1] = $b]', b=item)
print [r.strip() for r in result]
针对有问题的 HTML 片段进行测试时的输出:
['" A lot of important text"']
['"A lot of important text"']
['"1. List of text pertaining to Title3 "', '"2. List of items for Title 3"', '"3. the number of listed items is variable for every page"', '"4. Sometimes no list at all"']
[]
关于XPath的简要说明:
following-sibling::text()[normalize-space()]
: 查找非空的、跟随兄弟的文本节点...[preceding-sibling::b[1] = $b]
:...最近的前同级b
元素等于$b
。$b
是一个 XPath 参数,在上面的代码中被替换为当前的item
。这由xpath()
方法的第二个参数指示 (b=item
)