【问题标题】:xpath get the text from multi linesxpath 从多行中获取文本
【发布时间】:2014-03-24 21:32:41
【问题描述】:

我有这个 html

<td width="70%">REGEN REAL ESTATE, Dubai – U.A.E

RERA ID: 12087

Specialist Licensed Property Brokers &amp; Consultants
Residential / Commercial – Buying, Selling, R <a href="http://www.justproperty.com/company_view/index/3963">...Read more...</a></td>

我想获取td中的所有文本

我尝试了什么?

normalize-space(td/text())

但我只有最后一行。

我应该怎么做才能得到所有的行?

【问题讨论】:

    标签: python python-2.7 xpath scrapy


    【解决方案1】:

    您可以使用u"".join(selector.xpath('.//td//text()').extract())u"".join(selector.css('td ::text').extract())

    我差点忘了最简单的方法,如果你想要特定节点的每一个文本内容,你可以直接在上面使用normalize-space()

    paul@wheezy:~$ ipython
    Python 2.7.3 (default, Jan  2 2013, 13:56:14) 
    Type "copyright", "credits" or "license" for more information.
    
    IPython 0.13.1 -- An enhanced Interactive Python.
    ?         -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help      -> Python's own help system.
    object?   -> Details about 'object', use 'object??' for extra details.
    
    In [1]: from scrapy.selector import Selector
    
    In [2]: selector = Selector(text="""<td width="70%">REGEN REAL ESTATE, Dubai – U.A.E
       ...: 
       ...: RERA ID: 12087
       ...: 
       ...: Specialist Licensed Property Brokers &amp; Consultants
       ...: Residential / Commercial – Buying, Selling, R <a href="http://www.justproperty.com/company_view/index/3963">...Read more...</a></td>""", type="html")
    
    In [3]: selector.xpath("normalize-space(.//td)")
    Out[3]: [<Selector xpath='normalize-space(.//td)' data=u'REGEN REAL ESTATE, Dubai \u2013 U.A.E RERA ID'>]
    
    In [4]: selector.xpath("normalize-space(.//td)").extract()
    Out[4]: [u'REGEN REAL ESTATE, Dubai \u2013 U.A.E RERA ID: 12087 Specialist Licensed Property Brokers & Consultants Residential / Commercial \u2013 Buying, Selling, R ...Read more...']
    
    In [5]: [td.xpath("normalize-space(.)").extract() for td in selector.css("td")]
    Out[5]: [[u'REGEN REAL ESTATE, Dubai \u2013 U.A.E RERA ID: 12087 Specialist Licensed Property Brokers & Consultants Residential / Commercial \u2013 Buying, Selling, R ...Read more...']]
    
    In [7]: 
    

    请记住,normalize-space() 只会考虑您提供的节点集中的第一个节点作为参数,因此如果您确定您的参数将匹配您想要的一个且只有一个节点,它通常会执行您想要的操作。

    【讨论】:

    • 当scrapy之王到来时,我们都竖起耳朵。非常感谢。想念你 bty :)
    • @MarcoDinatsoli,如果您有 1 个特定节点,您可以在其上调用 normalize-space() 以连接所有后代文本节点。我编辑了答案以显示如何
    • 嗨@pault。 ,我经常看到你关于scrapy的回答。谢谢你的回答。请你检查我的问题here。提前致谢。
    • @WilliamKinaan,我问过我在 Scrapinghub 的同事
    【解决方案2】:

    normalize-space(//td/text()) 为我工作。

    演示(使用 xmllint):

    $ xmllint input.xml --xpath "normalize-space(//td/text())"
    REGEN REAL ESTATE, Dubai – U.A.E RERA ID: 12087 Specialist Licensed Property Brokers & Consultants Residential / Commercial – Buying, Selling, R
    

    input.xml 包含您提供的 xml。

    【讨论】:

    • 问题是normalize-space(//td/text()) 只会采用//td/text() 节点集的第一个节点。所以你得到td的第一个后代文本节点的空白规范化字符串,所以你不会得到“...阅读更多...”
    • @alecxe,远非如此 :) 我只知道一些 XPath 技巧。对我来说还有很多东西要学。你也是个明星;)
    • @pault。无论如何,非常感谢scrapy标签中的帮助,问题通常在标签中没有得到足够的关注。
    猜你喜欢
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多