×

Python Scrapy Object of type 'item' is not JSON serializable Scrapy报错报错Object of type is not JSON serializable

使用Scrapy时json.dumps报错Object of type 'item' is not JSON serializable

mtsym mtsym 发表于2019-03-01 23:38:03 浏览5478 评论0

抢沙发发表评论

给养的爬虫扩展了点功能,在分析数据导出结果的时候,使用json.dumps导出一个dict对象,结果报错

Object of type 'item' is not JSON serializable

这个dict对象很简单,里面有三个dict,每个dict里面有一个list,每个list里面是一个Scrapy.Item,奇怪的是其他item都没有报错,这个类型的item我检查了所有数据,确定是没有问题,很奇怪,于是尝试使用scrapy的serialize ScrapyJSONEncoder,发现是可以序列化的

from scrapy.utils.serialize import ScrapyJSONEncoder
encoder = ScrapyJSONEncoder()

encoder.encode(xxx_dict)

群贤毕至

访客