【问题标题】:How to extract text field with css using Xpath如何使用 Xpath 提取带有 css 的文本字段
【发布时间】:2020-12-01 05:18:12
【问题描述】:

我正在尝试使用下面的 xpath 表达式从 html 中提取价格。

<div class="-hr -pvs -mtxs" data-prd-prices="">
    ::before
    <span dir="ltr" data-price="" class="-b -ltr -tal -fs24">₦ 3,550</span>

</div>

my_xpath = //div[@class='-hr -pvs -mtxs']/span/text()

xpath 表达式定位价格,但是当我尝试抓取价格时,我得到了这个

 [<Selector xpath="//div[@class='-hr -pvs -mtxs']/span/text()" data='₦ 3,550'>]

我知道这是来自 html 标记中的 ::before。这是什么意思,我该如何提取价格

当我将 ::before 悬停在 chrome 开发人员工具中时,我得到了这个

div.-hr -pvs -mtxs::before

【问题讨论】:

    标签: python html web-scraping scrapy


    【解决方案1】:

    你做了正确的事,但你没有提取数据。

    my_xpath = "//div[@class='-hr -pvs -mtxs']/span/text()"
    extracted_data = response.xpath(my_xpath).getall()
    

    应该做的工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-13
      • 1970-01-01
      • 2016-10-02
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多