导入错误:没有模块名称 urllib2 [英] Import error: No module name urllib2
本文介绍了导入错误:没有模块名称 urllib2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
import urllib2.request
response = urllib2.urlopen("http://www.google.com")
html = response.read()
print(html)
有什么帮助吗?
推荐答案
urllib2
模块在 Python 3 中被拆分为多个名为 urllib.request
和 urllib.error
的模块.2to3
工具将在将您的源代码转换为 Python 3 时自动调整导入.
The
urllib2
module has been split across several modules in Python 3 namedurllib.request
andurllib.error
. The2to3
tool will automatically adapt imports when converting your sources to 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/")
.
Your current, now-edited code sample is incorrect because you are saying urllib.urlopen("http://www.google.com/")
instead of just urlopen("http://www.google.com/")
.
这篇关于导入错误:没有模块名称 urllib2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文