通过使用XPath创建的响应进行解析

时间:2019-06-12 11:50:58

标签: xpath scrapy-shell

我希望使用Scrapy从HTML格式正确的网站中提取一些数据。使用XPath,我可以提取项目列表,但不能使用XPath从列表中的元素中提取额外的数据

所有XPath均已使用XPather测试。我已经使用包含网页的本地文件测试了该问题,同样的问题。

在这里:

# Get the webpage
fetch("https://www.someurl.com")

# The following gives me the expected items from the HTML
products = response.xpath("//*[@id='product-list-146620']/div/div")

项目如下:

<div data-pageindex="1" data-guid="13157582" class="col ">
  <div class="item item-card item-card--static">
    <div class="item-card__inner">
      <div class="item__image item__image--overlay">
        <a href="/www.something.anywhere?ref_gr=9801" class="ratio_custom" style="padding-bottom:100%">
        </a>
      </div>
      <div class="item__text-container">
        <div class="item__name">
          <a class="item__name-link" href="/c.aspx?ref_gr=9801">The text I want</a>
        </div>
      </div>
    </div>
  </div>
</div>

当使用以下Xpath提取“我想要的文本”时,我什么也没得到:

XPATH_PRODUCT_NAME = "/div/div/div/div/div[contains(@class,'item__name')]/a/text()"
products[0].xpath(XPATH_PRODUCT_NAME).extract()

输出为空,为什么?

1 个答案:

答案 0 :(得分:0)

尝试以下代码。

XPATH_PRODUCT_NAME = ".//div[@class='item__name']/a[@class='item__name-link']/text()"
products[0].xpath(XPATH_PRODUCT_NAME).extract()
相关问题