urls = "https://arxiv.org/%s" %(str(url))
ress = requests.get(urls)
# ress.encoding = "gbk"
# print(ress.text.encode('utf8'))
# exit()
# ress.encoding= "gbk2312"
json_response = ress.text # 获取ress的文本 就是一个json字符串
chapter.append({'content':json_response})
with open('ComputerScience.json', 'a',encoding='utf-8') as fp: # 将所得的数据存储为json文件
fp.write(json.dumps(chapter, ensure_ascii=False, indent=4, sort_keys=True))
第一个问题:TypeError: Object of type 'Response' is not JSON serializable
因为我直接将ress请求过来的response直接传给了字典。
之前是
chapter.append({'content':ress})
改了一下就OK了:
json_response = ress.text # 获取ress的文本 就是一个json字符串
chapter.append({'content':json_response})
参考文章:https://www.cnblogs.com/Lin-Yi/p/7640147.html
第二个问题:'utf-8' codec can't decode byte 0xbf in position 10: invalid start byte编码问题。
第三个问题:UnicodeEncodeError: 'gbk' codec can't encode character '\ufffd' in position 670: illegal multibyte sequence
之前是
with open('ComputerScience.json', 'a') as fp:
改过之后的代码是:
with open('ComputerScience.json', 'a',encoding='utf-8') as fp:
这里参考了这篇文章:https://www.cnblogs.com/themost/p/6603409.html
文章:https://blog.csdn.net/jiang_1603/article/details/77856720