在 Python 2.7 中尝试从 urllib.request 导入 Request 时,找不到包。
>>> from urllib.request import Request
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named request
请您参考如下方法:
Python 2 中没有urllib.request 模块,那个模块only exists in Python 3 .
使用urllib2相反:
from urllib2 import Request
从模块文档的顶部开始:
Note: The
urllib2module has been split across several modules in Python 3 namedurllib.requestandurllib.error. The2to3tool will automatically adapt imports when converting your sources to Python 3.
如果您正在学习某种教程或类(class),您可能想要安装 Python 3 并继续使用该版本;您尝试执行的代码显然是为 Python 3 设计的。
