Scrapy仅显示每页的第一个结果

时间:2017-04-30 03:35:53

标签: scrapy scrapy-spider

我需要抓第一页的项目,然后转到下一个按钮转到第二页并刮擦等等。

这是我的代码,但只抓每页的第一项,如果有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

1 个答案:

答案 0 :(得分:0)

由于你没有给出一个工作目标网址,我在这里有点猜测,但很可能这就是问题所在:

resize.Thumbnail应为parse_item (并据此采取行动)

Scrapy正在下载一个完整的页面,根据您的描述,它有多个项目,然后将其作为响应对象传递给您的解析方法。

您的解析方法是通过迭代页面上显示的项目并相应地创建多个已删除项目来处理整个页面的责任。

scrapy文档有几个很好的例子,一个在这里:https://doc.scrapy.org/en/latest/topics/selectors.html#working-with-relative-xpaths

基本上,parse_page中的代码结构应如下所示:

def parse_XYZ

插入正确的xpath以获取页面上的所有项目并调整项目xpaths,您就可以开始了。