【问题标题】:Scrapy Crawler only pulls 19 of 680+ urlsScrapy Crawler 仅提取 680 多个网址中的 19 个
【发布时间】:2017-03-30 06:35:37
【问题描述】:

我正在尝试抓取此页面:https://coinmarketcap.com/currencies/views/all/

在所有行的td[2] 中是一个链接。我试图让scrapy 转到td 中的每个链接,并抓取链接所代表的页面。以下是我的代码:

注意:另一个人非常棒地帮助我走到了这一步

class ToScrapeSpiderXPath(CrawlSpider):
    name = 'coinmarketcap'
    start_urls = [
        'https://coinmarketcap.com/currencies/views/all/'
    ]

    rules = (
        Rule(LinkExtractor(restrict_xpaths=('//td[2]/a',)), callback="parse", follow=True),
    )

    def parse(self, response):
        BTC = BTCItem()
        BTC['source'] = str(response.request.url).split("/")[2]
        BTC['asset'] = str(response.request.url).split("/")[4],
        BTC['asset_price'] = response.xpath('//*[@id="quote_price"]/text()').extract(),
        BTC['asset_price_change'] = response.xpath(
            '/html/body/div[2]/div/div[1]/div[3]/div[2]/span[2]/text()').extract(),
        BTC['BTC_price'] = response.xpath('/html/body/div[2]/div/div[1]/div[3]/div[2]/small[1]/text()').extract(),
        BTC['Prct_change'] = response.xpath('/html/body/div[2]/div/div[1]/div[3]/div[2]/small[2]/text()').extract()
        yield (BTC)

即使表格超过 600 多个链接/页面,当我运行 scrapy crawl coinmarketcap 时,我也只得到 19 条记录。这意味着 600+ 的列表中只有 19 页。我没有看到停止刮擦的问题。任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 请分享您的爬取日志,尤其是末尾的 stats dict,您可以在其中查看已安排的请求数量、可能被过滤的数量、您获得的各种状态代码等。跨度>
  • 如果将回调从 parse() 更改为 parse_item()(并在规则中也调整回调名称)怎么办?

标签: python scrapy


【解决方案1】:

你的蜘蛛走得太深了:根据这条规则,它也会在单个硬币的页面中找到并跟踪链接。您可以通过添加 DEPTH_LIMIT = 1 来大致解决问题,但您肯定可以找到更优雅的解决方案。 这里是对我有用的代码(还有其他小的调整):

class ToScrapeSpiderXPath(CrawlSpider):
    name = 'coinmarketcap'
    start_urls = [
        'https://coinmarketcap.com/currencies/views/all/'
    ]
    custom_settings = {
        'DEPTH_LIMIT': '1',
    }

    rules = (
        Rule(LinkExtractor(restrict_xpaths=('//td[2]',)),callback="parse_item", follow=True),
    )

    def parse_item(self, response):
        BTC = BTCItem()
        BTC['source'] = str(response.request.url).split("/")[2]
        BTC['asset'] = str(response.request.url).split("/")[4]
        BTC['asset_price'] = response.xpath('//*[@id="quote_price"]/text()').extract()
        BTC['asset_price_change'] = response.xpath(
            '/html/body/div[2]/div/div[1]/div[3]/div[2]/span[2]/text()').extract()
        BTC['BTC_price'] = response.xpath('/html/body/div[2]/div/div[1]/div[3]/div[2]/small[1]/text()').extract()
        BTC['Prct_change'] = response.xpath('/html/body/div[2]/div/div[1]/div[3]/div[2]/small[2]/text()').extract()
        yield (BTC)

【讨论】:

  • 你先生真棒!!!!有用!。告诉我,除了 Depth_limit,你还改变了什么?
  • 根据 alecxe 的建议,我将回调从 parse() 更改为 parse_item() 并删除了一些不必要的逗号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多