【发布时间】:2016-02-21 13:36:03
【问题描述】:
我想要一个 XPath 来获取特定节点和子节点中包含的所有文本。
在下面的示例中,我试图获得:“Neil Carmichael (Stroud) (Con):”
<p>
<a class="anchor" name="qn_o0"> </a>
<a class="anchor" name="160210-0001.htm_wqn0"> </a>
<a class="anchor" name="160210109000034"> </a>
1. <a class="anchor" name="160210109000555"> </a>
<b><b>Neil Carmichael</b>
"(Stroud) (Con):"
</b>
"What assessment he has made of the value to the economy in Scotland of UK membership of the single market. [903484]"
</p>
到目前为止,我已经设法使用以下代码仅获得了一部分:
from lxml import html
import requests
page = requests.get('http://www.publications.parliament.uk/pa/cm201516/cmhansrd/cm160210/debtext/160210-0001.htm')
tree = html.fromstring(page.content)
test2 = tree.xpath('//div[@id="content-small"]/p[(a[@name[starts-with(.,"st_o")]] or a[@name[starts-with(.,"qn_")]])]/b/text()')
欢迎任何帮助!
【问题讨论】:
标签: xml xpath web-scraping