【问题标题】:conditional xpath statement条件 xpath 语句
【发布时间】:2017-09-01 18:02:06
【问题描述】:

这是一段 HTML,我想从中提取信息:

  <li>
    <p><strong class="more-details-section-header">Provenance</strong></p>
    <p>Galerie Max Hetzler, Berlin<br>Acquired from the above by the present owner</p>
  </li>

我想要一个 xpath 表达式来提取第二个 &lt;p&gt; ... &lt;/p&gt; 的内容,这取决于之前是否有 &lt;p&gt; ... Provenance ... &lt;/p&gt; 的兄弟姐妹

这是我到目前为止的位置:

if "Provenance" in response.xpath('//strong[@class="more-details-section-header"]/text()').extract():
            print("provenance = yes")

但是我怎么去Galerie Max Hetzler, Berlin&lt;br&gt;Acquired from the above by the present owner?

我试过了

if "Provenance" in response.xpath('//strong[@class="more-details-section-header"]/text()').extract():
            print("provenance = yes ", response.xpath('//strong[@class="more-details-section-header"]/following-sibling::p').extract())

但我得到[]

【问题讨论】:

    标签: python-3.x xpath web-scraping scrapy


    【解决方案1】:

    你应该使用

    //p[preceding-sibling::p[1]/strong='Provenance']/text()
    

    【讨论】:

    • 或者更准确地说 "//p[preceding-sibling::p[1]='Provenance']/text()"
    猜你喜欢
    • 2011-12-05
    • 2019-06-02
    • 2012-12-06
    • 2012-02-22
    • 1970-01-01
    • 2010-11-12
    • 2016-06-25
    • 2013-03-09
    • 2011-05-29
    相关资源
    最近更新 更多