响应在 Scrapy Shell 中有效,但在代码中不起作用



我是新来的。我为这个网站https://book24.ru/knigi-bestsellery/?section_id=1592写了我的第一个蜘蛛,它工作得很好

import scrapy

class BookSpider(scrapy.Spider):
name = 'book24'
start_urls = ['https://book24.ru/knigi-bestsellery/']

def parse(self, response):

for link in response.css('div.product-card__image-holder a::attr(href)'):
yield response.follow(link, callback=self.parse_book)

for i in range (1, 5):
next_page = f'https://book24.ru/knigi-bestsellery/page-{i}/'
yield response.follow(next_page, callback=self.parse)
print(i)

def parse_book(self, response):
yield{
'name': response.css('h1.product-detail-page__title::text').get(),
'type': response.css('div.product-characteristic__value a::attr(title)')[2].get()
}

现在我试着为一个页面写一个蜘蛛

import scrapy

class BookSpider(scrapy.Spider):
name = 'book'
start_urls = ['https://book24.ru/product/transhumanism-inc-6015821/']

def parse_book(self, response):
yield{
'name': response.css('h1.product-detail-page__title::text').get(),
'type': response.css('div.product-characteristic__value a::attr(title)')[2].get()
}

它不工作,我得到一个空文件后,这个命令在终端。

scrapy crawl book -O book.csv

我不知道为什么。将感激您的帮助!

你要加薪了

NotImplementedError(f'{self.__class__.__name__}.parse callback is not defined')
NotImplementedError: BookSpider.parse callback is not defined

根据文件

parse():将被调用来处理响应的方法为每个请求下载。响应参数为anTextResponse的实例,该实例保存了页面内容并具有进一步的有用的处理方法

parse()方法通常解析响应,提取被抓取的内容数据作为字典,还可以查找要遵循的新url并创建新的url

重新命名你的def parse_book(self, response): to def parse(self, response):

相关内容

最新更新