scrapy基礎知識之將item寫入JSON文件:


pipelines.py

import json

class xxPipeline(object):
     def __init__(self):
         self.filename=open("xx.json","wb")
     def process_item(self, item, spider):
         jsontext=json.dumps(dict(item),ensure_ascii=False) + ",\n"
         self.filename.write(jsontext.encode("utf-8"))
         return item
     def close_spider(self,spider):
           self.filename.close()

 注意:須將它的類添加到 settings.py文件ITEM_PIPELINES 配置

ITEM_PIPELINES = {"mySpider.pipelines.xxPipeline":300

也可通過命令
scrapy crawl xx(爬蟲名字) -o xx.json
scrapy crawl xx(爬蟲名字) -o xx.csv

scrapy crawl xx(爬蟲名字) -o xx.xml



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM