Scrapy nie indeksuje kolejnych stron w kolejności
Piszę robota, aby uzyskać nazwy przedmiotów ze strony internetowej. Witryna ma 25 elementów na stronie i wiele stron (200 dla niektórych typów elementów).
Oto kod:
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
Po uruchomieniu przeszukiwacza i zapisaniu danych w formacie csv dane nie są przechowywane w porządku, tj. - dane strony 2 są przechowywane przed zapisaniem strony 1 lub 3 przed stroną 2 i podobnie. Czasami, zanim wszystkie dane danej strony zostaną zapisane, pojawią się dane innej strony, a pozostałe dane poprzedniej strony zostaną ponownie zapisane.