需要为 Python 3.5.1 安装 urllib2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34475051/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:03:32  来源:igfitidea点击:

Need to install urllib2 for Python 3.5.1

pythonpython-3.xurllib2

提问by Eamonn Gormley

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.

我正在为 Mac 运行 Python 3.5.1。我想使用 urllib2。我尝试安装它,但我被告知它已被拆分为 Python 3 的 urllib.request 和 urllib.error。

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

我的命令(现在从框架 bin 目录运行,因为它不在我的路径中):

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.

在我尝试一口气安装 urllib2 之前,我遇到了同样的错误。

回答by Ilya V. Schurov

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.

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

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

因此,似乎不可能完全按照您的意愿去做,但是您可以使用urllib.request.

回答by Martijn Pieters

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 urllibsome time after June 2017, remove that package as soon as possible.

警告:安全研究人员在 PyPI 上发现了几个中毒的包,包括一个名为 的包urllib,安装后会“打电话回家”。如果您pip install urllib在 2017 年 6 月之后使用了一段时间,请尽快移除该软件包

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

你不能,也没有必要。

urllib2is the name of the library included in Python 2. You can use the urllib.requestlibraryincluded with Python 3, instead. The urllib.requestlibrary works the same way urllib2works in Python 2. Because it is already includedyou don't need to install it.

urllib2是 Python 2 中包含的库的名称。您可以改用Python 3 中包含的urllib.request。该urllib.request库的工作方式urllib2与 Python 2 中的工作方式相同。因为它已经包含在内,所以您无需安装它。

If you are following a tutorial that tells you to use urllib2then 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 urllib2comes with that version.

如果您正在学习指导您使用的教程,urllib2那么您会发现会遇到更多问题。您的教程是为 Python 2 而不是 Python 3 编写的。查找其他教程,或安装 Python 2.7 并在该版本上继续您的教程。你会发现urllib2带有那个版本。

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

或者,安装requests以获得更高级别且更易于使用的 API。它适用于 Python 2 和 3。

回答by dschinn1001

At beginning of your python script should be following:

在你的 python 脚本的开头应该如下:

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:

或者您在“导入 urllib2”之后继续使用这个免费示例,如下所示:

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