Python爬取网页乱码解决

设置requests返回值编码:

获取返回值编码:res.encoding    :ISO-8859-1

获取文本内容编码:res.apparent_encoding  :utf-8

新的字符串str1用于接收编码后的字符串:

str1 = res.text.encode(res.encoding).decode(res.apparent_encoding)

 

 

 

以utf-8编码方式打开文件。

f = open("file.txt","w",encoding='utf-8')  

posted @ 2020-09-22 19:33  棂信  阅读(380)  评论(0编辑  收藏  举报