导入错误:没有名为“urllib2”的模块 Python 3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34740288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImportError: No module named 'urllib2' Python 3
提问by Jordi Salom
The below code is working fine on Python 2 but on Python 3 I get the error:
以下代码在 Python 2 上运行良好,但在 Python 3 上出现错误:
"ImportError: No module named 'urllib2'"
“导入错误:没有名为‘urllib2’的模块”
import urllib2
peticion = 'I'm XML'
url_test = 'I'm URL'
req = urllib2.Request(url=url_test,
data=peticion,
headers={'Content-Type': 'application/xml'})
respuesta = urllib2.urlopen(req)
print(respuesta)
print(respuesta.read())
respuesta.open()
Please suggest me the reason of error.
请建议我错误的原因。
Thank you.
谢谢你。
采纳答案by Prashant Puri
check StackOverflow Link
import urllib.request
url = "http://www.google.com/"
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
print (response.read().decode('utf-8'))
回答by Alexander Ejbekov
The urllib and urllib2 modules are merged together in python3 as urllib. If you want to make your code compatible with both 2.x and 3.x, I would advise you to look into the six module
urllib 和 urllib2 模块在 python3 中合并为 urllib。如果你想让你的代码同时兼容 2.x 和 3.x,我建议你查看六个模块