`没有名为'urllib2'的模块-我如何在Python中使用它以便发出请求

问题描述 投票:0回答:1

Python新手,只是不确定要为python做什么

我想使用urllib2-Request拨打电话我该怎么做,例如在repl中。我找不到正确的方法

$ python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from urllib2 import Request, urlopen, URLerror, HTTPerror
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'urllib2'
>>> from urllib2 import Request, urlopen, URLerror, HTTPerror
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'urllib2'
>>> import Request
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'Request'
>>> import urllib2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'urllib2'
>>> 

...
req = Request(URL, json_message) #(URL and json_message defined but not shown here)

...

我是否必须将urllib2单独安装到系统中或进行其他安装。就像我说的那样,只是Python新手不知道步骤和语法。谢谢!

我正在工作的示例具有

from urllib2 import Request, urlopen, URLError, HTTPError

然后使用Request(...,但是当我在python3 repl中尝试该操作时,我得到了

$ python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from urllib2 import Request
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'urllib2'
>>> 
python-3.x import urllib2
1个回答
0
投票

[python3中没有urllib2;有关更多详细信息,请参见this question。 (这里背景故事的简短版本是Python2和Python3完全是完全不同的类型; Py2中并非所有的stdlib库在Py3中都可用。)

相反,请尝试urllib(类似的API);

from urllib import request

您可以访问urllib文档here,可能会有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.