Need to install urllib2 for Python 3.5.1

我的未来我决定 提交于 2019-12-29 11:40:15

问题


I'm running Python 3.5.1 for Mac. I want to use urllib2. I tried installing that but I'm told that it's been split into urllib.request and urllib.error for Python 3.

My command (running from the framework bin directory for now because it's not in my path):

sudo ./pip3 install urllib.request

Returns:

Could not find a version that satisfies the requirement urllib.request (from versions: )
No matching distribution found for urllib.request

I got the same error before when I tried to install urllib2 in one fell swoop.


回答1:


WARNING: Security researches have found several poisoned packages on PyPI, including a package named urllib, which will 'phone home' when installed. If you used pip install urllib some time after June 2017, remove that package as soon as possible.

You can't, and you don't need to.

urllib2 is the name of the library included in Python 2. You can use the urllib.request library included with Python 3, instead. The urllib.request library works the same way urllib2 works in Python 2. Because it is already included you don't need to install it.

If you are following a tutorial that tells you to use urllib2 then you'll find you'll run into more issues. Your tutorial was written for Python 2, not Python 3. Find a different tutorial, or install Python 2.7 and continue your tutorial on that version. You'll find urllib2 comes with that version.

Alternatively, install the requests library for a higher-level and easier to use API. It'll work on both Python 2 and 3.




回答2:


As per docs:

Note. The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.

So it seems that it is impossible to do exactly what you want but you can use appropriate python3 functions from urllib.request.




回答3:


At beginning of your python script should be following:

import urllib2

after this you can proceed with an free example like following:

response = urllib2.urlopen('http://pythonforbeginners.com/')
print response.info()
html = response.read()
response.close()  # best practice to close the file

or you proceed after 'import urllib2' with this free example as follows:

page = urllib2.urlopen('http://0.0.0.0')
print page.info()


来源:https://stackoverflow.com/questions/34475051/need-to-install-urllib2-for-python-3-5-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!