为怪异的标题道歉(难以解释)。这是我的 HTML:
<p>
<strong>Date:</strong> May 12, 2015
</p>
基本上,我想提取日期(2015 年 5 月 12 日)。我想出了:
print advisory.xpath('//p/strong[text()="Date:"]')[0].text
但这自然会返回 Date:。知道如何遍历父级,跳过强标签中的 w/e 并返回其余部分吗?
请您参考如下方法:
或者,由于日期文本实际上是 <strong> 的后文元素,你可以使用 tail像这样的属性:
[2]: s = '''<root><p><strong>Date:</strong> May 12, 2015</p></root>'''
In [3]: from lxml import etree as ET
In [4]: tree = ET.fromstring(s)
In [5]: tree.xpath('//p/strong[text()="Date:"]')[0].tail
Out[5]: ' May 12, 2015'
