Scrapy不按顺序爬行后续页面

Sou*_*Das 3 python web-crawler scrapy

我正在编写一个搜寻器以从网站获取项目名称。该网站每页有25个项目,多页(某些项目类型为200个)。

这是代码:

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from lonelyplanet.items import LonelyplanetItem

class LonelyplanetSpider(CrawlSpider):
    name = "lonelyplanetItemName_spider"
    allowed_domains = ["lonelyplanet.com"]
    def start_requests(self):
        for i in xrange(8):
            yield self.make_requests_from_url("http://www.lonelyplanet.com/europe/sights?page=%d" % i)

def parse(self, response):
    hxs = HtmlXPathSelector(response)
    sites = hxs.select('//h2')
    items = []
    for site in sites:
        item = LonelyplanetItem()
        item['name'] = site.select('a[@class="targetUrl"]/text()').extract()
        items.append(item)
    return items
Run Code Online (Sandbox Code Playgroud)

当我运行搜寻器并以csv格式存储数据时,数据没有按顺序存储,即-第2页数据存储在第1页之前,而第3页存储在第2页之前,类似地。同样有时,在存储特定页面的所有数据之前,还会进入另一页面的数据,并且将前一页的其余数据再次存储。

str*_*nac 5

scrapy是一个异步框架。它使用非阻塞IO,因此它不会在开始下一个请求之前等待请求完成。

而且由于一次可以发出多个请求,所以不可能知道该parse()方法将获得响应的确切顺序。

我的观点是,scrapy并不意味着按特定顺序提取数据。如果您绝对需要保留订单,则可以在此处提供一些建议: 在订单中抓取抓取网址