python3.8 不能使用requests问题

2020-04-04  本文已影响0人  AISpider

初次安装python3.8后,使用py抓取网页,代码中会使用

import requests
...

运行代码会提示

ModuleNotFoundError: No module named 'requests'

问题:因为你的python还未安装requests库,需要通过pip安装。

(venv) E:\dev\uspider>pip install requests
Collecting requests
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x0000021F85E8A760
>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/requests/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x0000021F85E8A340
>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/requests/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x0000021F85EACDC0
>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/requests/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x0000021F85EACAC0
>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/requests/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x0000021F85EAC610
>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/requests/
  Could not find a version that satisfies the requirement requests (from versions: )
No matching distribution found for requests

从执行的过程来看,请求一直处于超时。这是由于国内很多情况下,无法正常去下载国外服务器资源导致。
解决方法:使用国内镜像,常用的有镜像有

使用镜像的安装范式为:

pip install xxxx -i http://… 或者 pip install  -i https://… xxxx。
# 示例如下:
pip install requests -i https://mirrors.aliyun.com/pypi/simple/

注意:如果安装过程中提示需要升级pip

You should consider upgrading via the 'python -m pip install --upgrade pip' command.
# 解决命令
python -m pip install --upgrade pip -i http://mirrors.aliyun.com/pypi/simple/

当安装完requests后,测试通过,OK!

>>>
>>> import requests
>>>
上一篇 下一篇

猜你喜欢

热点阅读