当前位置:主页 > 软件编程 > Python代码 >

解决python3爬虫无法显示中文的问题

时间:2021-10-07 10:59:51 | 栏目:Python代码 | 点击:

有时候使用python从网站上爬数据的时候,如果数据里包含中文,有时候显示的却是如下所示...\xe4\xba\xba\xef\xbc\x8c\xe6...类似与国际化

解决方法:

import urllib.request
import sys

weburl="..."
webhead=...
req=urllib.request.Request(url=weburl,headers=webhead)
response=urllib.request.urlopen(req)
content = response.read()
#获得系统的编码
type = sys.getfilesystemencoding()
#设置爬出内容的编码
content = content.decode(type)
file = open("c。txt",'w',10000)
file.write(str(content))
file.close()
print(content)

您可能感兴趣的文章:

相关文章