解决scrapy下载小说乱序
生活随笔
收集整理的這篇文章主要介紹了
解决scrapy下载小说乱序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
由于scrapy使用異步下載,所以會出現下載小說章節的結果是亂序的。
可以通過下面的方法將章節順序傳遞給item,并保存起來:
在解析主頁得到所有章節信息(章節名、網址、還有順序)后,通過Request()的cb_kwargs傳遞一個關鍵字參數‘order’給回調函數parse_item(),代表該章節的順序。
items.py:
# -*- coding: utf-8 -*- import scrapyclass XiaoshuoItem(scrapy.Item):order = scrapy.Field() # 序號,章節排序的依據name = scrapy.Field() # 章節名content = scrapy.Field() # 章節內容xiaoshuo_spyder.py:
# -*- coding: utf-8 -*- import scrapy from scrapy import Request from Xiaoshuo.items import XiaoshuoItemclass XiaoshuoSpider(scrapy.Spider):name = 'Xiaoshuo_spider'start_urls = ['https://www.biquge.biz/0_844/']def parse(self, response):"""解析主頁里所有章節地址并下載,通過cb_kwargs={'order': i + 1}來傳遞章節順序"""sels = response.xpath('//div[@id="list"]//dd/a')for i, a in enumerate(sels):# yield response.follow(a, callback=self.parse_item, cb_kwargs={'order': i + 1})yield Request(response.urljoin(a.xpath('@href').get()), callback=self.pasrse_item, cb_kwargs={'order': i + 1})def parse_item(self, response, order):"""主頁解析后章節順序通過order傳遞進來"""item = XiaoshuoItem()item['order'] = orderitem['name'] = response.xpath('//h1/text()').get()item['content'] = response.xpath('//div[@id="content"]').get()return itempipelines.py
# -*- coding: utf-8 -*- class XiaoshuoPipeline(object):def open_spider(self, spider):"""定義items,用來保存每個item"""self.items = []def process_item(self, item, spider):"""將下載解析到的各個item添加到items,此時是亂序的"""self.items.append(item)return itemdef close_spider(self, spider):"""在爬蟲結束的時候,將items按照'order'字段排列,并最終合并成一個html文件"""with open('御魂者傳奇.html', 'w', encoding='utf-8') as f:header = '<!DOCTYPE html><html lang="zh"><head><meta charset="UTF-8"></head><body>'footer = '</body></html>'f.write(header)# 所有章節按order字段排序self.items.sort(key=lambda i: i['order'])for item in self.items:cont = '<h3>{}</h3>{}<br><hr><br>'.format(item['name'], item['content'])f.write(cont)f.write(footer)settings.py:(打開管道)
# Configure item pipelines # See https://docs.scrapy.org/en/latest/topics/item-pipeline.html ITEM_PIPELINES = {'project_name.pipelines.XiaoshuoPipeline': 300, }如果本文對您有幫助,請給我留個言。
總結
以上是生活随笔為你收集整理的解决scrapy下载小说乱序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言双人贪吃蛇游戏瘦身版本
- 下一篇: asp.net建站