Scrapy爬虫什么告诉我这个输出?

问题描述 投票:0回答:1

我的scrapy测试代码如下。我通过scrapy shell测试它,它工作。但是现在,如果我最终开始编写脚本,则不会出现输出。有什么不对?谢谢。

我的代码到目前为止:

import scrapy


class CryptohunterSpider(scrapy.Spider):
    name = "cryptohunter"
    start_urls=["https://www.coingecko.com/de"]



def parse(self, response):

    for x in response.xpath('//div[@class="coin-content center"]/a/span/text()'):
        print (x.extract())

输出:抱歉我无法输入shell,所以我开始截图。

所以我不确定这是不是错误。如果它应该是一个错误,我如何处理它?

如何获得全部数字?它总是舍入2.输出:0,07而不是0.0688852511227967

python scrapy web-crawler
1个回答
1
投票

Python中的缩进非常重要(more info about indentation)。

不正确的缩进可能导致错误或程序执行错误。在您的情况下,parse()方法不属于您的类。所以在执行期间,scrapy试图在parse()中找到CryptohunterSpider方法并失败并出现错误:

raise NotImplementedError('{}.parse callback is not defined'.format(self.__class__.__name__))

您需要正确缩进代码,以便parse()属于该类

import scrapy

class CryptohunterSpider(scrapy.Spider):
    name = "cryptohunter"
    start_urls=["https://www.coingecko.com/de"]

    def parse(self, response):
        for x in response.xpath('//div[@class="coin-content center"]/a/span/text()'):
            print ("Extract: ", x.extract())
© www.soinside.com 2019 - 2024. All rights reserved.