小编Sou*_*Das的帖子

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页之前,类似地。同样有时,在存储特定页面的所有数据之前,还会进入另一页面的数据,并且将前一页的其余数据再次存储。

python web-crawler scrapy

3
推荐指数
1
解决办法
2775
查看次数

标签 统计

python ×1

scrapy ×1

web-crawler ×1