PythonWebScraping字符映射问题,在空闲状态下可以正常工作,而不是在AtomIDE中?为什么?

2024-10-01 02:34:18 发布

您现在位置:Python中文网/ 问答频道 /正文

在从python执行简单的url读取请求时。我在Atom IDE中遇到了以下问题,在空闲时我没有收到错误,原因可能是什么。下面是代码

from urllib.request import urlopen
html = urlopen("https://morvanzhou.github.io/static/scraping/basic-structure.html").read().decode('utf-8')
print(html)

以下是错误: enter image description here


Tags: 代码fromhttpsimporturlrequesthtml错误
1条回答
网友
1楼 · 发布于 2024-10-01 02:34:18

这种情况很可能发生,因为Atom的终端需要一种编码,而python则将该html编码为另一种编码。你在Windows上使用Atom吗

要解决此问题,请尝试将Atom终端配置为系统编码或将html编码为utf8:

from urllib.request import urlopen
html = urlopen("https://morvanzhou.github.io/static/scraping/basic-structure.html").read().decode('utf-8')
print(html.encode('utf-8'))

相关问题 更多 >