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'
>>>
答案 0 :(得分:1)
python3中没有urllib2;有关更多详细信息,请参见this question。 (这里背景的简短版本是Python2和Python3完全是完全不同的类型;并非Py2中的所有stdlib库在Py3中都可用。)
相反,请尝试urllib
(类似的API);
from urllib import request
您可以访问urllib文档here,这可能会有所帮助。