【问题标题】:Problem finding correct Selector for response.xpath or response.css in Scrapy on Coinmarketcap在 Coinmarketcap 上的 Scrapy 中为 response.xpath 或 response.css 找到正确的选择器时出现问题
【发布时间】:2020-02-16 10:53:27
【问题描述】:

我想遍历 coinmarketcap 上的 top20 交易所来爬取表格,例如https://coinmarketcap.com/exchanges/fatbtc/

现在我花了几个小时寻找选择器,例如价格

在 Scrapy Shell 中,我尝试了...等等,但都不起作用:

来自插件XPath Helper

response.xpath('/html/body/div[@id='__next']/div[@class='cmc-app-wrapper.cmc-app-wrapper--env-prod.sc-1mezg3x-0.fUoBLh']/div[@class='container.cmc-main-section']/div[@class='cmc-main-section__content']/div[@class='cmc-exchanges.sc-1tluhf0-0.wNRWa']/div[@class='cmc-details-panel-table.sc-3klef5-0.cSzKTI']/div[@class='cmc-markets-listing.lgsxp9-0.eCrwnv']/div[@class='cmc-table.sc-1yv6u5n-0.dNLqEp']/div[@class='cmc-table__table-wrapper-outer']/div/table/tbody/tr[@class='cmc-table-row.sc-1ebpa92-0.kQmhAn'][1]/td[@class='cmc-table__cell.cmc-table__cell--sortable.cmc-table__cell--right.cmc-table__cell--sort-by__price']').getall()

来自 Chrome 检查器:

response.xpath('/td[@class='cmc-table__cell.cmc-table__cell--sortable.cmc-table__cell--right.cmc-table__cell--sort-by__price']').getall()

来自 Chrome 检查器复制 XPath: :

response.xpath('//*[@id="__next"]/div/div[2]/div[1]/div[2]/div[2]/div/div[2]/div[3]/div/table/tbody/tr[1]/td[5]').extract()

我正在使用 Chrome Inspector,并且从今天开始使用一个名为“Xpath helper”的插件来显示选择器,但我仍然不明白我在那里做什么:(。我真的很感激任何想法访问该数据并让我更好地了解如何找到这些选择器。

【问题讨论】:

    标签: xpath scrapy css-selectors


    【解决方案1】:

    XPATH 基本上是 HTML 中的 //tagname[@attribute='value']。

    对于您的网站,您可以使用 //table[@id='exchange-markets']//tr/td[2]/a 循环访问名称

    通过//table[@id='exchange-markets']//tr/td[5]获取价格

    我们基本上是说在第 5 列的表格行中查看。

    【讨论】:

      【解决方案2】:

      很简单(我用position() 跳过表头):

      for row in response.xpath('//table[@id="exchange-markets"]//tr[position() > 1]'):
          price = row.xpath('.//span[@class="price"]/text()').get()
      #    price = row.xpath('.//span[@class="price"]/@data-usd').get() #if you need to be more precise
      

      【讨论】:

      • 非常感谢。它的工作。不知道为什么,但我的 Chrome Dev Inspector 没有显示我的 ID。奇怪的! Startet Edge 就在那里......
      猜你喜欢
      • 1970-01-01
      • 2018-01-08
      • 1970-01-01
      • 2021-10-13
      • 2019-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多