Python 刮削分页定时错误
所以我设置了一个蜘蛛,非常类似于scrapy上的示例 我希望蜘蛛在进入下一页之前抓取所有引用。我还希望它每秒只解析1个引号。因此,如果一页上有20个引号,则需要20秒来刮取引号,然后1秒才能转到下一页 到目前为止,我当前的实现是在实际获取报价信息之前先遍历每个页面Python 刮削分页定时错误,python,pagination,scrapy,Python,Pagination,Scrapy,所以我设置了一个蜘蛛,非常类似于scrapy上的示例 我希望蜘蛛在进入下一页之前抓取所有引用。我还希望它每秒只解析1个引号。因此,如果一页上有20个引号,则需要20秒来刮取引号,然后1秒才能转到下一页 到目前为止,我当前的实现是在实际获取报价信息之前先遍历每个页面 import scrapy class AuthorSpider(scrapy.Spider): name = 'author' start_urls = ['http://quotes.toscrape.com/'] def
import scrapy
class AuthorSpider(scrapy.Spider):
name = 'author'
start_urls = ['http://quotes.toscrape.com/']
def parse(self, response):
# follow links to author pages
for href in response.css('.author+a::attr(href)').extract():
yield scrapy.Request(response.urljoin(href),
callback=self.parse_author)
# follow pagination links
next_page = response.css('li.next a::attr(href)').extract_first()
if next_page is not None:
next_page = response.urljoin(next_page)
yield scrapy.Request(next_page, callback=self.parse)
def parse_author(self, response):
def extract_with_css(query):
return response.css(query).extract_first().strip()
yield {
'name': extract_with_css('h3.author-title::text'),
'birthdate': extract_with_css('.author-born-date::text'),
'bio': extract_with_css('.author-description::text'),
}
以下是我的settings.py文件的基本内容
ROBOTSTXT_OBEY = True
CONCURRENT_REQUESTS = 1
DOWNLOAD_DELAY = 2
您可以协调如何生成scrapy.request 例如,您可以创建下一个页面请求,但只有在所有作者请求终止并删除其项目时才会生成该请求 例如:
import scrapy
# Store common info about pending request
pending_authors = {}
class AuthorSpider(scrapy.Spider):
name = 'author'
start_urls = ['http://quotes.toscrape.com/']
def parse(self, response):
# process pagination links
next_page = response.css('li.next a::attr(href)').extract_first()
next_page_request = None
if next_page is not None:
next_page = response.urljoin(next_page)
# Create the Request object, but does not yield it now
next_page_request = scrapy.Request(next_page, callback=self.parse)
# Requests scrapping of authors, and pass reference to the Request for next page
for href in response.css('.author+a::attr(href)').extract():
pending_authors[href] = False # Marks this author as 'not processed'
yield scrapy.Request(response.urljoin(href), callback=self.parse_author,
meta={'next_page_request': next_page_request})
def parse_author(self, response):
def extract_with_css(query):
return response.css(query).extract_first().strip()
item = {
'name': extract_with_css('h3.author-title::text'),
'birthdate': extract_with_css('.author-born-date::text'),
'bio': extract_with_css('.author-description::text'),
}
# marks this author as 'processed'
pending_authors[response.url] = True
# checks if finished processing of all authors
if len([value for key, value in pending_authors.iteritems() if value == False]) == 0:
yield item
next_page_request = response.meta['next_page_request']
# Requests next page, after finishinr all authors
yield next_page_request
else:
yield item
似乎不起作用。全局名称
pending_authors
未定义。JK它应该是self.pending_authors,用于它的所有实例。后续问题:我注意到,使用此方法,如果它看到已解析的页面,它将不会尝试转到下一个链接。您知道如何禁用此功能(不要刮取以前见过的作者,但仍会在页面中迭代)?刚刚编辑了我的答案,“待定作者”是指全局性的,而不是针对每个实例。关于后续问题:这只是一个示例,说明您可以做些什么来协调刮取请求;您可以通过url以外的其他方式保存作者。我也做过类似的事情,但我使用mongodb来存储这些项目,当我进行刮取时,我可以在保存刮取的数据之前先进行检查。谢谢您的帮助!然而,我注意到了这个实现中的一些事情。1.必须在parse和parse_author 2中全局调用挂起的_authors。在这个实现中,如果您看到重复的作者(您以前在以前的scrape中解析过的作者),除非您在middleware.py中将全局挂起的作者dict中的键设置为true,否则将不会处理它