scrapy-reids 中提示‘AttributeError: 'DoubanSpider' object has no attribute 'make_requests_from_url'’

运行scrapy-redis时出现这个问题(scrapy runspider ...)

img

buteError: 'DoubanSpider' object has no attribute 'make_requests_from_url'

卸载过scrapy-redis也没用

scrapy版本问题, pip install -U Scrapy==2.5.1 , 实测可解决

前面其他人说的换版本,我没试,但说一下我的版本,Scrapy 2.6.1,scrapy-redis,0.7.2。
我在爬虫文件中导入了from scrapy.linkextractors import LinkExtractor和from scrapy.spiders import Rule
我的rules:

rules = (
        Rule(page_number_url, follow=True),
        Rule(movie_detail_url, callback='parse_item', follow=False)
)

按照前面其他人说的,自己在spider中重写make_requests_from_url()方法,他说这样写:

def make_requests_from_url(self, url):
    return scrapy.Request(url=url, callback=self.parse_item, )

我的parse_item()方法是从response中根据xpath获取数据,
然后终端执行scrapy runspider 爬虫文件名(含扩展名),
Redis中执行lpush 类名(全小写):start_urls "开始爬取的url(即第一页的url)",
报错了,大概意思是xpath返回的列表,用下标取数据时越界了。
然后我在parse_item()方法中手动将response.text写入文件,打开文件一看,原来内容是开始爬取的url(Redis命令中的网址)的内容,不是我xpath提取数据的页面,即make_requests_from_url()方法将开始爬取的url的对应响应通过回调传给了parse_item()方法,而我的parse_item()方法里的xpath是针对第二个Rule匹配到的链接的对应响应的,所以我又改成下面的代码:

def make_requests_from_url(self, url):
    yield scrapy.Request(url=url)

即让它只发送请求,不设置回调函数,这样我的第二个Rule匹配到的链接就能将对应的响应正确传给回调函数parse_item()方法了,Redis中也有数据了。

最后,为什么会遇到object has no attribute 'make_requests_from_url'这个报错?scrapy的Spider()类中start_requests()方法中已移除对make_requests_from_url()方法的调用,且类中也移除了make_requests_from_url()方法,而scrapy-redis中调用了这个现在不存在的make_requests_from_url()方法,它在scrapy-redis的spiders.py中RedisMixin()类中make_request_from_data()方法末尾的return self.make_requests_from_url(url),只出现了这一次,其他地方没找到make_requests_from_url。
移除前的源码在https://www.cnblogs.com/zhaof/p/7192503.html

参考链接
https://github.com/rmax/scrapy-redis/issues/181
https://github.com/rmax/scrapy-redis/issues/150
https://github.com/rmax/scrapy-redis/issues/142
https://github.com/rmax/scrapy-redis/issues/92
https://www.cnblogs.com/xiaowangba9494/p/14298923.html
https://blog.csdn.net/weixin_42866931/article/details/111825036

在settings里面把ITEM_PIPELINES值设置为400,CONCURRENT_REQUESTS 设置为10。