导入错误:没有名为"urllib2"的模块 Python 3



下面的代码在Python 2上运行良好,但在Python 3上我得到了错误:

"ImportError:没有名为'urlib2'的模块"

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()

请告诉我出错的原因。

谢谢。

检查StackOverflow链接

import urllib.request
url = "http://www.google.com/"
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
print (response.read().decode('utf-8'))

urllib和urllib2模块在python3中合并为urllib。如果你想让你的代码同时兼容2.x和3.x,我建议你研究一下六模块

最新更新