【发布时间】:2016-06-15 08:31:59
【问题描述】:
我正在开发一个简单的刮板来获取 9 个恶作剧帖子及其图像,但由于一些技术困难,我无法停止刮板并且它一直在刮,这是我不想要的。我想增加计数器值并在 100 个帖子后停止。 但是 9gag 页面的设计方式是在每个响应中只提供 10 个帖子,并且在每次迭代后我的计数器值重置为 10,在这种情况下,我的循环运行无限长并且永不停止。
# -*- coding: utf-8 -*-
import scrapy
from _9gag.items import GagItem
class FirstSpider(scrapy.Spider):
name = "first"
allowed_domains = ["9gag.com"]
start_urls = (
'http://www.9gag.com/',
)
last_gag_id = None
def parse(self, response):
count = 0
for article in response.xpath('//article'):
gag_id = article.xpath('@data-entry-id').extract()
count +=1
if gag_id:
if (count != 100):
last_gag_id = gag_id[0]
ninegag_item = GagItem()
ninegag_item['entry_id'] = gag_id[0]
ninegag_item['url'] = article.xpath('@data-entry-url').extract()[0]
ninegag_item['votes'] = article.xpath('@data-entry-votes').extract()[0]
ninegag_item['comments'] = article.xpath('@data-entry-comments').extract()[0]
ninegag_item['title'] = article.xpath('.//h2/a/text()').extract()[0].strip()
ninegag_item['img_url'] = article.xpath('.//div[1]/a/img/@src').extract()
yield ninegag_item
else:
break
next_url = 'http://9gag.com/?id=%s&c=200' % last_gag_id
yield scrapy.Request(url=next_url, callback=self.parse)
print count
items.py 的代码在这里
from scrapy.item import Item, Field
class GagItem(Item):
entry_id = Field()
url = Field()
votes = Field()
comments = Field()
title = Field()
img_url = Field()
所以我想增加一个全局计数值并尝试通过将 3 个参数传递给解析函数它给出错误
TypeError: parse() takes exactly 3 arguments (2 given)
那么有没有办法传递一个 全局计数 值并在每次迭代后返回它并在 100 个帖子后停止(假设)。
整个项目都可以在这里找到Github 即使我设置 POST_LIMIT =100 也会发生无限循环,请参阅此处执行的命令
scrapy crawl first -s POST_LIMIT=10 --output=output.json
【问题讨论】:
标签: python python-2.7 loops python-3.x scrapy