Scrapy 显示 notImplementedError,我不知道为什么 [英] Scrapy is showing notImplementedError and I don't know why

查看:53
本文介绍了Scrapy 显示 notImplementedError,我不知道为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Scrapy 代码不起作用,我不知道为什么.我的蜘蛛是在 Reddit 上爬取权力的游戏 subreddit 的测试.

My Scrapy code doesn't work and I am not sure why. My spider is a test to crawl the Game of Throne subreddit on Reddit.

这是我的代码:

import scrapy


class Redditbot2Spider(scrapy.Spider):
    name = 'redditbot2'
    allowed_domains = ['www.reddit.com']
    start_urls = ['https://www.reddit.com/r/gameofthrones/']


def parse(self, response):
    titles = response.selector.xpath('//h2/text()').extract()
    votes = response.selector.xpath('//div[@class="_1rZYMD_4xY3gRcSS3p80D0"]/test()').extract()
    time = response.selector.xpath('//a[@class="_3jOxDPIQ0KaOWpzvSQo-1s"]/text()').extract()
    comments = response.selector.xpath('//span[@class="FHCV02u6Cp2zYL0fhQPsO"])/text()').extract()

    for item in zip(titles, votes, time, comments):
        scraped_info = {
            'title': titles[0],
            'vote': votes[1],
            'time': time[2],
            'comments': comments[3],
        }
        yield scraped_info

这里是错误日志:

Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "/Library/Python/2.7/site-packages/scrapy/spiders/__init__.py", line 90, in parse
    raise NotImplementedError('{}.parse callback is not defined'.format(self.__class__.__name__))
NotImplementedError: Redditbot2Spider.parse callback is not defined

Redditbot2Spider.parse 方法就在那里,所以我不明白它为什么这么说.有什么想法吗?

The Redditbot2Spider.parse method is there, so I don't get why it is saying that. Any ideas?

推荐答案

你的代码有缩进问题,parse 方法与类处于同一级别,所以解释器没有意识到它是类的成员.您必须在 parse 方法中缩进:

Your code has an indentation problem, the parse method is in the same level as the class is, so the interpreter doesn't realize that it is a member of the class. You have to make an indent in the parse method:

class Redditbot2Spider(scrapy.Spider): 
    name = 'redditbot2'
    allowed_domains = ['www.reddit.com']
    start_urls = ['https://www.reddit.com/r/gameofthrones/']
    def parse(self, response): 
        titles = response.selector.xpath('//h2/text()').extract()
        #etc.

这篇关于Scrapy 显示 notImplementedError,我不知道为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆