将爬取的文章转JSON存储遇到的坑

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值