【发布时间】:2017-06-03 05:45:16
【问题描述】:
所以我设置了一个蜘蛛,和scrapy上的例子非常相似。
我希望蜘蛛在进入下一页之前抓取所有引号。我还希望它每秒只解析 1 个报价。因此,如果一个页面上有 20 条引号,则抓取引号需要 20 秒,然后需要 1 秒才能转到下一页。
截至目前,我当前的实现是在实际获取报价信息之前先遍历每个页面。
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
【问题讨论】:
标签: python pagination scrapy