来自urllib2导入请求 - 无法导入名称请求

问题描述 投票:6回答:4

所以我正在尝试编写一个程序,我需要这个名为请求的模块,因为我使用的是python版本2.7.9。

我想使用代码:

from urllib2 import request

会做的。然而,我得到以下错误:

Traceback (most recent call last):
  File "C:\Python27\NLTKHoofdstuk3.py", line 5, in <module>
    from urllib2 import request
ImportError: cannot import name request

我没有得到它,因为我有urllib2完全在Python上工作,模块请求应该在模块urllib2(当我输入“import urllib2”时我也没有得到错误)

所以有人知道为什么我的模块请求不起作用,但我的模块urllib2是

python python-2.7 module request urllib2
4个回答
11
投票

尝试使用

from urllib2 import Request

代替

from urllib2 import request

检查官方documentation


11
投票

运用

import urllib;

然后

urllib.urllopen();

对于Python 2.7,您不需要import请求。


1
投票

尝试使用:

import urllib2

并使用urllib2代替urllib.request

看文件https://docs.python.org/2/library/urllib2.html#urllib2.urlopen可能会有所帮助。


0
投票

我使用的是Python 2.7并遇到了同样的问题。在使用urllib3而不是urlliburllib2后,我能够解决它。

以下是在Python 2.7中使用urllib3的示例屏幕截图:jupyter_Screenshot_for_urllib3

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