|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
当执行如下代码时:(由于没有发送带url的权限的帖子, 链接中间用空格隔开)
import urllib.request
resp = urllib.request.urlopen('https:// fishc . com. cn')
html = resp.read()
html = html.decode("utf-8")
#解码,将其变为unicode编码,即还原为带中文的html代码
print(html)
运行上述代码提示如下错误:
Traceback (most recent call last):
File "D:/program/python_code/14_1.py", line 5, in <module>
html = html.decode("utf-8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd3 in position 252: invalid continuation byte
请大神指点下错误原因, 谢谢
本帖最后由 jackz007 于 2024-11-16 23:32 编辑
- html = html.decode("utf-8")
复制代码
改为:
- html = html.decode("gbk")
复制代码
- import urllib . request
- resp = urllib . request . urlopen('https://fishc.com.cn')
- html = resp . read()
- html = html . decode('gbk')
- fp = open('fishc.html' , 'w')
- fp . write(html)
- fp . close()
复制代码
在源代码同目录下,用 Chrome 或 EDGE 浏览器打开新生成的 'fishc.html' 文件
|
|