【问题标题】:Only extract information from the div class if it contains a certain word using xpath仅当 div 类包含使用 xpath 的某个单词时才从 div 类中提取信息
【发布时间】:2019-07-16 14:35:04
【问题描述】:

我正在尝试从以下网站https://www.rawson.co.za抓取信息

但是,有时,信息会改变它的位置。 我正在努力只检查“建筑物大小”并将其存储为大小,因为 div 类看起来像这样:

<div class="features__item">
            <div class="features__icon icon-house" aria-hidden="true"></div>
        <div class="features__label">Building Size 130m²</div>
</div>

我能够提取它,但有时它需要其他信息,因为该属性要么没有它,要么有其他东西在它的位置。

这就是我现在拥有的尺寸(我正在从子/属性页面访问信息):

size = response.xpath("//div[@class='features']/div[@class='features__list']/div[@class='row']/div[@class='col col--1-2'][2]/div[@class='features__item'][1]/div[@class='features__label']/text()").re(r'\d+')[0]

我想获取的是建筑物大小信息(仅数字)(如果存在),如果没有可用的建筑物大小,则输入 None。我正在努力处理 div 类中的文本部分。我试图构建一个 for 循环来检查它是否包含“建筑大小”,但还没有任何效果。任何帮助将不胜感激!谢谢!

【问题讨论】:

    标签: python xpath web-scraping scrapy


    【解决方案1】:

    简单:

    size = response.xpath("//div[@class='features__label'][contains(., 'Building Size')]/text()").re_first(r'\d+')
    

    【讨论】:

    • 非常感谢! @gangabas
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多