Requests 简明教程

Requests - Environment Setup

在本章中,我们将致力于 Requests 的安装。要开始使用 Requests 模块,我们首先需要安装 Python。所以我们要按如下进行 −

In this chapter, we will work on the installation of Requests. To start working with the Requests module, we need to install Python first. So we are going to work on following −

  1. Install Python

  2. Install Requests

Installing Python

转到 Python 官方网站: https://www.python.org/downloads/ ,如下所示,然后单击适用于 Windows、Linux/Unix 和 Mac OS 的最新可用版本。根据您现有的 64 位或 32 位操作系统下载 Python。

Go to the Python official site: https://www.python.org/downloads/ as shown below and click on the latest version available for Windows, Linux/Unix, and Mac OS. Download Python as per your 64 or 32 bit OS available with you.

python download

下载完成后,单击 .exe 文件并按照步骤在您的系统上安装 Python。

Once you have downloaded, click on the .exe file and follow the steps to install python on your system.

python for windows

Python 包管理器(即 pip)也将默认随上述安装一起安装。要使它在您的系统上全局工作,请直接将 Python 的位置添加到 PATH 变量。在安装开始时显示相同内容,请记住勾选“添加到 PATH”复选框。如果您忘记选中它,请按照以下给定的步骤添加到 PATH。

The python package manager, i.e., pip will also get installed by default with the above installation. To make it work globally on your system, directly add the location of python to the PATH variable. The same is shown at the start of the installation to remember to check the checkbox which says ADD to PATH. In case you forget to check it, please follow the below-given steps to add to PATH.

To add to PATH follow the steps −

右键单击您的计算机图标,然后单击属性>高级系统设置。

Right-click on your Computer icon and click on properties > Advanced System Settings.

它会显示如下屏幕:

It will display the screen as shown below −

system properties

单击“环境变量”,如下图所示,它会显示如下屏幕:

Click on Environment Variables as shown above. It will display the screen as shown below −

environment variables

选择路径并单击编辑按钮,在末尾添加您的 Python 的位置路径。现在,让我们检查 python 版本。

Select Path and click on Edit button, add the location path of your python at the end. Now, let us check the python version.

Checking the python version

E:\prequests>python --version
Python 3.7.3

Install Requests

既然我们已经安装了 Python,我们将安装 Requests。

Now that we have python installed, we are going to install Requests.

安装 Python 后,也会安装 Python 包管理器(即 pip)。以下是检查 pip 版本的命令。

Once python is installed, python package manager i.e. pip will also get installed. Following is the command to check pip version.

E:\prequests>pip --version
pip 19.1.1 from c:\users\xxxxx\appdata\local\programs\python\python37\lib\site-
packages\pip (python 3.7)

我们已安装了 pip 并且版本为 19.1.1。现在,将使用 pip 安装 Requests 模块。

We have pip installed and the version is 19.1.1. Now, will use pip to install Requests module.

下面给出了命令 −

The command is given below −

pip install requests
E:\prequests>pip install requests
Requirement already satisfied: requests in c:\users\xxxx\appdata\local\programs
\python\python37\lib\site-packages (2.22.0)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\kamat\appdata\local\
programs\python\python37\lib\site-packages (from requests) (2019.3.9)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\use
rs\xxxxx\appdata\local\programs\python\python37\lib\site-packages (from requests
) (1.25.3)
Requirement already satisfied: idna<2.9,>=2.5 in c:\users\xxxxxxx\appdata\local\
programs\python\python37\lib\site-packages (from requests) (2.8)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\users\xxxxx\appdata\
local\programs\python\python37\lib\site-packages (from requests) (3.0.4)

我们已经安装了该模块,因此在命令提示符中,它显示 Requirement already satisfied(需求已满足);如果没有安装,它将已为安装下载所需的包。

We already have the module installed, so in the command prompt it says Requirement already satisfied; if not installed it would have downloaded the required packages for installation.

要检查所安装请求模块的详细信息,可以使用以下命令 −

To check the details of the requests module installed, you can use the following command −

pip show requests
E:\prequests>pip show requests
Name: requests
Version: 2.22.0
Summary: Python HTTP for Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: me@kennethreitz.org
License: Apache 2.0
Location: c:\users\xxxxx\appdata\local\programs\python\python37\lib\site-package
S
Requires: certifi, idna, urllib3, chardet
Required-by:

Requests 模块的版本为 2.22.0。

The version of Requests module is 2.22.0.