本文介绍了`没有名为'urllib2'的模块-我如何在Python中使用它以便发出请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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'
>>>
解决方案

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

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

from urllib import request

您可以在此处中找到urllib文档,这可能会有所帮助. /p>

Python newbie and just not sure what to do for python

I want to use urllib2 - Request to make a callHow can I do that, for example in the repl.I can't figure out the right way

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

...

Do I have to install urllib2 into the system separately or something.Like I said just a Python newbie not knowing steps and syntax. thanks!

The example I am working from has

from urllib2 import Request, urlopen, URLError, HTTPError

and then uses Request(... but when I try that in a python3 repl I get

$ 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'
>>>
解决方案

There is no urllib2 in python3; see this question for more details. (The short version of the backstory here is that Python2 and Python3 are entirely different types of flying altogether; not all stdlib libraries in Py2 are available in Py3.)

Instead, try urllib (similar API);

from urllib import request

You can hit the urllib documentation here, which may help.

这篇关于`没有名为'urllib2'的模块-我如何在Python中使用它以便发出请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:14