类型错误:“字节"类型的对象不是 JSON 可序列化的 python3 [英] TypeError: Object of type 'bytes' is not JSON serializable python3
本文介绍了类型错误:“字节"类型的对象不是 JSON 可序列化的 python3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在运行我的蜘蛛后收到此错误我也有一个管道,我将所有内容都转换为 JSON,但在我的项目返回后仍然出现此错误
i received this error after running my spider i also have a pipeline and i convert everything into JSON but still got this error after my item return
TypeError: 'bytes' 类型的对象不是 JSON 可序列化的
TypeError: Object of type 'bytes' is not JSON serializable
我的代码是
import json
import re
import types
SEPARATOR = '-'
FILING_PROPERTIES = ['state_id', 'types', 'description', 'filing_parties', 'filed_on']
DOCUMENT_PROPERTIES = ['types', 'title', 'blob_name', 'state_id', 'source_url']
class AeeiPipeline(object):
def process_item(self, item, spider):
import pdb
#
if item.get('title', None):
item['source_title'], item['title'] = self.title_case(item['title'])
if item.get('description'):
pdb.set_trace()
item['description'] = self.title_case(item['description'])
for filing in item.get("filings", []):
if filing.get('description'):
pdb.set_trace()
filing['description'] = self.title_case(filing['description'])
for _key in ["filing_parties", "types"]:
if not (_key in filing and filing[_key]):
filing[_key] = []
elif isinstance(filing[_key], str):
filing[_key] = [filing[_key]]
for doc in filing.get("documents", []):
if doc.get('name'):
doc['name'] = doc['name']
if doc.get('title'):
doc['title'] = self.make_unicode(doc['title'])
if "types" in doc and not type(doc["types"]) is list:
doc["types"] = [doc["types"]]
for _key in ["industries", "assignees", "major_parties", "source_assignees", "source_major_parties"]:
if not (_key in item and item[_key]):
item[_key] = []
elif isinstance(item[_key], str):
item[_key] = [item[_key]]
for key, value in item.items():
if type(item[key]) is str:
item[key] = value.strip()
pdb.set_trace()
item = json.dumps(item) + '\n'
return item
def title_case(self, title):
title = self.make_unicode(title)
return title, re.sub(u"[A-Za-z]+(('|\u2019)[A-Za-z]+)?",
lambda mo: mo.group(0)[0].upper() + mo.group(0)[1:].lower(),
title)
推荐答案
TypeError: Object of type 'PucItem' is not JSON serializable
这意味着您正在使用 Scrapy 的 Item 类
This means you are using Scrapy's Item class
解决方案是要么这样做
item = json.dumps(dict(item))
或者在你的 Spider 中,不要使用 Item 类来创建项目,只需使用像 item = {}
Or in your Spider, do NOT use Item class to create item, just use a Dict like item = {}
这篇关于类型错误:“字节"类型的对象不是 JSON 可序列化的 python3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文