我刚开始编程 Python。我想用scrapy创建一个bot,运行项目时显示TypeError: Object of type ‘bytes’ is not JSON serializable。
import json
import codecs
class W3SchoolPipeline(object):
def __init__(self):
self.file = codecs.open('w3school_data_utf8.json', 'wb', encoding='utf-8')
def process_item(self, item, spider):
line = json.dumps(dict(item)) + '\n'
# print line
self.file.write(line.decode("unicode_escape"))
return item
from scrapy.spiders import Spider
from scrapy.selector import Selector
from w3school.items import W3schoolItem
class W3schoolSpider(Spider):
name = "w3school"
allowed_domains = ["w3school.com.cn"]
start_urls = [
"http://www.w3school.com.cn/xml/xml_syntax.asp"
]
def parse(self, response):
sel = Selector(response)
sites = sel.xpath('//div[@id="navsecond"]/div[@id="course"]/ul[1]/li')
items = []
for site in sites:
item = W3schoolItem()
title = site.xpath('a/text()').extract()
link = site.xpath('a/@href').extract()
desc = site.xpath('a/@title').extract()
item['title'] = [t.encode('utf-8') for t in title]
item['link'] = [l.encode('utf-8') for l in link]
item['desc'] = [d.encode('utf-8') for d in desc]
items.append(item)
return items
追溯:
TypeError: Object of type 'bytes' is not JSON serializable
2017-06-23 01:41:15 [scrapy.core.scraper] ERROR: Error processing {'desc': [b'\x
e4\xbd\xbf\xe7\x94\xa8 XSLT \xe6\x98\xbe\xe7\xa4\xba XML'],
'link': [b'/xml/xml_xsl.asp'],
'title': [b'XML XSLT']}
Traceback (most recent call last):
File
"c:\users\administrator\appdata\local\programs\python\python36\lib\site-p
ackages\twisted\internet\defer.py", line 653, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "D:\LZZZZB\w3school\w3school\pipelines.py", line 19, in process_item
line = json.dumps(dict(item)) + '\n'
File
"c:\users\administrator\appdata\local\programs\python\python36\lib\json_
_init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File
"c:\users\administrator\appdata\local\programs\python\python36\lib\json\e
ncoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File
"c:\users\administrator\appdata\local\programs\python\python36\lib\json\e
ncoder.py", line 257, in iterencode
return _iterencode(o, 0)
File
"c:\users\administrator\appdata\local\programs\python\python36\lib\
json\encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'bytes' is not JSON serializable
原文由 Zhibin 发布,翻译遵循 CC BY-SA 4.0 许可协议
您正在自己创建那些
bytes
对象:Each of those
t.encode()
,l.encode()
andd.encode()
calls creates abytes
string.不要这样做,将其留给 JSON 格式以序列化这些。接下来,您还犯了其他几个错误;你在没有必要的地方编码太多。将其留给
json
模块和open()
调用返回的 标准 文件对象来处理编码。您也不需要将
items
列表转换为字典;它已经是一个可以直接进行 JSON 编码的对象:我猜你遵循了假设 Python 2 的教程,你正在使用 Python 3。我强烈建议你找一个不同的教程;它不仅是为过时的 Python 版本编写的,如果它提倡
line.decode('unicode_escape')
它正在教导一些极坏的习惯,这些习惯会导致难以跟踪的错误。我可以推荐你看看 Think Python,第 2 版,这是一本关于学习 Python 3 的免费好书。