我有一个网站刮。在主页上有故事情节 - 所以,这个页面将成为我们的开始解析页面。我的蜘蛛从它那里收集关于每个故事的数据 - 作者,评级,出版日期等等。这一切都是由蜘蛛正确完成的。蟒蛇scrapy从几个页面收集数据到一个项目(字典)
import scrapy
from scrapy.spiders import Spider
from sxtl.items import SxtlItem
from scrapy.http.request import Request
class SxtlSpider(Spider):
name = "sxtl"
start_urls = ['some_site']
def parse(self, response):
list_of_stories = response.xpath('//div[@id and @class="storyBox"]')
item = SxtlItem()
for i in list_of_stories:
pre_rating = i.xpath('div[@class="storyDetail"]/div[@class="stor\
yDetailWrapper"]/div[@class="block rating_positive"]/span/\
text()').extract()
rating = float(("".join(pre_rating)).replace("+", ""))
link = "".join(i.xpath('div[@class="wrapSLT"]/div[@class="title\
Story"]/a/@href').extract())
if rating > 6:
yield Request("".join(link), meta={'item':item}, callback=\
self.parse_story)
else:
break
def parse_story(self, response):
item = response.meta['item']
number_of_pages = response.xpath('//div[@class="pNavig"]/a[@href]\
[last()-1]/text()').extract()
if number_of_pages:
item['number_of_pages'] = int("".join(number_of_pages))
else:
item['number_of_pages'] = 1
item['date'] = "".join(response.xpath('//span[@class="date"]\
/text()').extract()).strip()
item['author'] = "".join(response.xpath('//a[@class="author"]\
/text()').extract()).strip()
item['text'] = response.xpath('//div[@id="storyText"]/div\
[@itemprop="description"]/text() | //div[@id="storyText"]\
/div[@itemprop="description"]/p/text()').extract()
item['list_of_links'] = response.xpath('//div[@class="pNavig"]\
/a[@href]/@href').extract()
yield item
因此,数据收集正确,但我们只有每个故事的第一页。但是,每个莎莉都有几页(并且链接到第2,3,4页,有时15页)。这就是问题出现的地方。我这个替换产量项目:(让每一个故事的第2页)
yield Request("".join(item['list_of_links'][0]), meta={'item':item}, \
callback=self.get_text)
def get_text(self, response):
item = response.meta['item']
item['text'].extend(response.xpath('//div[@id="storyText"]/div\
[@itemprop="description"]/text() | //div[@id="storyText"]\
/div[@itemprop="description"]/p/text()').extract())
yield item
蜘蛛收集下一个(第二)页,但只将它们加入到任何故事的第一页。例如,第一个故事的第二页可能被添加到第四个故事。第5个故事的第2页被添加到第1个故事。等等。
请帮忙,如果要将数据收集到一个项目(一个字典),如果要抓取的数据在多个网页上传播? (在这种情况下 - 如何不让来自不同项目的数据彼此混合?)
谢谢。
您是否检查此链接:http://stackoverflow.com/questions/13910357/how-can-i-use-multiple-requests-and-pass-items-in-between-them-in-scrapy-python ? – Wandrille
@Wandrille我已经找到了解决方案,但感谢有趣的链接。 –