【问题标题】:Retrieve value from span class XPath从跨度类 XPath 中检索值
【发布时间】:2019-10-07 13:20:01
【问题描述】:

我正在尝试从该网站https://www.gumtree.co.zahttps://www.gumtree.co.za/a-house-rentals-flat-rentals-offered/tamboerskloof/studio-flatlet-in-tamboerskloof/1005754794350910092234609 这是我从中获取信息的财产的链接)中抓取一些信息;更具体地说,我正在尝试从这些跨度类中获取信息:

<div class="attribute">
  <span class="name">Bathrooms (#):</span>
  <span class="value">1</span>
</div>

我首先要检查 span 类中是否包含浴室,然后为其取值。这就是我现在拥有的:

bathrooms=response.xpath("//span[contains(text(),'Bathrooms')]/span[@class='value']text()").extract_first()

但是,我什么也没得到。

有什么建议吗?谢谢!

【问题讨论】:

  • 您的代码正在寻找卧室而不是浴室
  • @balderman 哦,我的错字,谢谢。仍然有同样的问题。

标签: python xpath web-scraping scrapy web-crawler


【解决方案1】:

这是提取所有兄弟的正确方法。 Bathrooms=response.xpath("//span[contains(text(),'Bathrooms')]/following-sibling::*").extract_first()

更多内容可以参考:XPath Axes 希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 2021-12-22
    • 2018-11-30
    • 2018-11-15
    • 2016-10-13
    • 1970-01-01
    相关资源
    最近更新 更多