我是靠谱客的博主 单纯羽毛,这篇文章主要介绍scrapy将数据载入json文件,现在分享给大家,希望可以做个参考。

import json
from itemadapter import ItemAdapter


class LianjiaPipeline:
    def open_spider(self, spider):
        self.file = open('lianjia.json', 'a', encoding='utf-8')

    def close_spider(self, spider):
        self.file.close()

    def process_item(self, item, spider):
        line = json.dumps(ItemAdapter(item).asdict(), ensure_ascii=False) + "n"
        self.file.write(line)
        return item

在pipeline文件加入如上代码

最后

以上就是单纯羽毛最近收集整理的关于scrapy将数据载入json文件的全部内容,更多相关scrapy将数据载入json文件内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(106)

评论列表共有 0 条评论

立即
投稿
返回
顶部