Scrapy只显示每页的第一个结果
Scrapy only show the first result of each page
我需要抓取第一页的项目然后转到下一个按钮转到第二页并抓取等等。
这是我的代码,但只抓取每页的第一项,如果有 20 页,则进入每个页面并只抓取第一项。
谁能帮帮我。
谢谢
为我的英语道歉。
class CcceSpider(CrawlSpider):
name = 'ccce'
item_count = 0
allowed_domain = ['www.example.com']
start_urls = ['https://www.example.com./afiliados value=&categoria=444&letter=']
rules = {
# Reglas Para cada item
Rule(LinkExtractor(allow = (), restrict_xpaths = ('//li[@class="pager-next"]/a')), callback = 'parse_item', follow = True),
}
def parse_item(self, response):
ml_item = CcceItem()
#info de producto
ml_item['nombre'] = response.xpath('normalize-space(//div[@class="news-col2"]/h2/text())').extract()
ml_item['url'] = response.xpath('normalize-space(//div[@class="website"]/a/text())').extract()
ml_item['correo'] = response.xpath('normalize-space(//div[@class="email"]/a/text())').extract()
ml_item['descripcion'] = response.xpath('normalize-space(//div[@class="news-col4"]/text())').extract()
self.item_count += 1
if self.item_count > 5:
#insert_table(ml_item)
raise CloseSpider('item_exceeded')
yield ml_item
因为你没有给出一个工作目标 url,我在这里有点猜测,但很可能是这个问题:
parse_item
应该是 parse_page
(并据此采取行动)
Scrapy 正在下载一个完整的页面,根据您的描述,该页面有多个项目,然后将其作为响应对象传递给您的解析方法。
您的解析方法负责通过遍历页面上显示的项目并相应地创建多个抓取的项目来处理整个页面。
scrapy 文档有几个很好的例子,一个在这里:https://doc.scrapy.org/en/latest/topics/selectors.html#working-with-relative-xpaths
基本上 def parse_XYZ
中的代码结构应该如下所示:
def parse_page(self, response):
items_on_page = response.xpath('//...')
for sel_item in items_on_page:
ml_item = CcceItem()
#info de producto
ml_item['nombre'] = # ...
# ...
yield ml_item
插入正确的 xpath 以获取页面上的所有项目并调整您的项目 xpath,然后您就可以开始了。
我需要抓取第一页的项目然后转到下一个按钮转到第二页并抓取等等。
这是我的代码,但只抓取每页的第一项,如果有 20 页,则进入每个页面并只抓取第一项。
谁能帮帮我。
谢谢
为我的英语道歉。
class CcceSpider(CrawlSpider):
name = 'ccce'
item_count = 0
allowed_domain = ['www.example.com']
start_urls = ['https://www.example.com./afiliados value=&categoria=444&letter=']
rules = {
# Reglas Para cada item
Rule(LinkExtractor(allow = (), restrict_xpaths = ('//li[@class="pager-next"]/a')), callback = 'parse_item', follow = True),
}
def parse_item(self, response):
ml_item = CcceItem()
#info de producto
ml_item['nombre'] = response.xpath('normalize-space(//div[@class="news-col2"]/h2/text())').extract()
ml_item['url'] = response.xpath('normalize-space(//div[@class="website"]/a/text())').extract()
ml_item['correo'] = response.xpath('normalize-space(//div[@class="email"]/a/text())').extract()
ml_item['descripcion'] = response.xpath('normalize-space(//div[@class="news-col4"]/text())').extract()
self.item_count += 1
if self.item_count > 5:
#insert_table(ml_item)
raise CloseSpider('item_exceeded')
yield ml_item
因为你没有给出一个工作目标 url,我在这里有点猜测,但很可能是这个问题:
parse_item
应该是 parse_page
(并据此采取行动)
Scrapy 正在下载一个完整的页面,根据您的描述,该页面有多个项目,然后将其作为响应对象传递给您的解析方法。
您的解析方法负责通过遍历页面上显示的项目并相应地创建多个抓取的项目来处理整个页面。
scrapy 文档有几个很好的例子,一个在这里:https://doc.scrapy.org/en/latest/topics/selectors.html#working-with-relative-xpaths
基本上 def parse_XYZ
中的代码结构应该如下所示:
def parse_page(self, response):
items_on_page = response.xpath('//...')
for sel_item in items_on_page:
ml_item = CcceItem()
#info de producto
ml_item['nombre'] = # ...
# ...
yield ml_item
插入正确的 xpath 以获取页面上的所有项目并调整您的项目 xpath,然后您就可以开始了。