【问题标题】:Scrapy not crawling subsequent pages in orderScrapy没有按顺序抓取后续页面
【发布时间】:2012-06-18 10:19:17
【问题描述】:

我正在编写一个爬虫来从网站获取项目的名称。该网站每页有 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

当我运行爬虫并以 csv 格式存储数据时,数据未按顺序存储,即 - 第 2 页数据存储在第 1 页之前或第 3 页存储在第 2 页之前,类似地。有时在存储特定页面的所有数据之前,另一个页面的数据会进入,并且前一页面的其余数据会再次存储。

【问题讨论】:

  • 请注意条款和条件 - lonelyplanet.com/legal/website-terms IANAL 但我认为这些意味着不允许爬行。
  • 我没有将内容用于商业目的。我正在使用该网站来学习使用爬虫。

标签: python web-crawler scrapy


【解决方案1】:

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

而且由于一次可以发出多个请求,因此无法知道parse() 方法获取响应的确切顺序。

我的意思是,scrapy 并不是要按特定顺序提取数据。如果您绝对需要保持秩序,这里有一些想法: Scrapy Crawl URLs in Order

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多