导入错误:没有模块名称 urllib2

2024-03-28

这是我的代码:

import urllib2.request

response = urllib2.urlopen("http://www.google.com")
html = response.read()
print(html)

有什么帮助吗?


如中所述urllib2文档 https://docs.python.org/2/library/urllib2.html:

The urllib2模块已被拆分为 Python 3 中的多个模块,名为urllib.request and urllib.error. The 2to3将源代码转换为 Python 3 时,工具会自动调整导入。

所以你应该说

from urllib.request import urlopen
html = urlopen("http://www.google.com/").read()
print(html)

您当前编辑的代码示例不正确,因为您说urllib.urlopen("http://www.google.com/")而不是仅仅urlopen("http://www.google.com/").

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

导入错误:没有模块名称 urllib2 的相关文章

随机推荐