Python 2.7无法从urllib.request中找到模块Request

Kep*_*ond 1 python urllib python-import python-2.7

当尝试Requesturllib.requestPython 2.7 导入时,它无法找到包.

>>> from urllib.request import Request
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named request
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 5

urllib.requestPython 2中没有模块,该模块仅存在于Python 3中.

urllib2改为使用:

from urllib2 import Request
Run Code Online (Sandbox Code Playgroud)

从模块顶部的文档:

注意:urllib2模块已经拆分为Python 3中的几个模块命名urllib.requesturllib.error.2to3将源转换为Python 3时,该工具将自动调整导入.

如果您正在学习某种教程或课程,您可能需要安装Python 3并继续使用该版本; 您尝试执行的代码显然是为Python 3设计的.