【问题标题】:XPath how to get child nodes text and selfXPath如何获取子节点文本和自我
【发布时间】: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


    【解决方案1】:

    /b 停止您的XPath,因此它返回&lt;b&gt; 元素而不是&lt;b&gt; 中的文本节点。然后您可以在每个元素上调用text_content() 以获得预期的文本输出,例如:

    from lxml import html
    
    raw = '''<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>'''
    
    root = html.fromstring(raw)
    result = root.xpath('//p/b')
    print result[0].text_content()
    
    # output :
    # 'Neil Carmichael\n     "(Stroud) (Con):"\n    '
    

    作为text_content() 的替代方案,您可以使用XPath string() 函数和可选的normalize-space()

    print result[0].xpath('string(normalize-space())')
    # output :
    # Neil Carmichael "(Stroud) (Con):"
    

    【讨论】:

    • 谢谢我尝试了类似的方法,但诀窍是使用 [0] 访问一个元素。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多