Requests 简明教程

Requests - Overview

Requests 是一个 HTTP 库,它提供了轻松处理 Web 应用程序中 http 请求/响应的功能。此库使用 python 开发。

Requests is a HTTP library that provides easy functionality to deal with http request/response in your web application. The library is developed in python.

Python Requests 的官方网站,可从 https://2.python-requests.org/en/master/ 获得,将 Requests 定义如下 −

The official website of Python Requests which is available at https://2.python-requests.org/en/master/ defines Requests as follows −

Requests 是 Python 中一个优雅简单的 HTTP 库,为人类而设计。

Requests is an elegant and simple HTTP library for Python, built for human beings.

Features of Requests

Requests 的功能在下面讨论 −

The features of Requests are discussed below −

Request

python 请求库提供了易于使用的可用来处理 Http 请求的方法。传递参数并处理 GET、POST、PUT、DELETE 等请求类型非常简单。

The python requests library has easy to use methods available to handle Http request. Passing of parameters and handling the request type like GET, POST, PUT, DELETE, etc. is very easy.

Response

您可以获取所需的格式的响应,支持的格式有文本格式、二进制响应、json 响应和原始响应。

You can get the response in the format you need and the supported ones are text format, binary response, json response, and raw response.

Headers

此库允许您根据您的要求读取、更新或发送新的标头。

The library allows you to read, update or send new headers as per your requirements.

Timeouts

使用 python requests 库,可以轻松地将超时添加到您请求的 URL。您碰巧使用的是第三方 URL 并等待响应。

Timeouts can be easily added to the URL you are requesting using python requests library. It so happens that you are using a third-party URL and waiting for a response.

在 URL 上给出一个超时总是一个好习惯,因为我们可能希望 URL 在该超时内通过响应或错误做出响应。不这样做可能会导致无限期等待该请求。

It is always a good practice to give a timeout on the URL as we might want the URL to respond within that timeout with a response or an error that is coming because of timeout. Not doing so can cause either to wait on that request indefinitely.

Error handling

requests 模块提供了对错误处理的支持,其中包括连接错误、超时错误、TooManyRedirects、Response.raise_for_status 错误等。

The requests module gives support for error handling and some of which are Connection Error, Timeout errors, TooManyRedirects, Response.raise_for_status errors, etc.

Cookies

该库允许您对请求的 URL 进行读取、写入和更新。

The library allows you to read, write and update for the requested URL.

Sessions

为了维护数据,您需要在请求之间进行会话。因此,如果反复调用同一个主机,您可以重用 TCP 连接,这反过来将提高性能。

To maintain the data, you require between requests you need sessions. So, if the same host is called again and again you can re-use the TCP connection which in turn will improve the performance.

SSL certificates

SSL 证书是一项随安全 url 而来的安全功能。当您使用 Requests 时,它还会验证给定 https URL 的 SSL 证书。Requests 库中默认启用了 SSL 验证,如果证书不存在,将抛出错误。

SSL certificate is a security feature that comes with secure urls. When you use Requests, it also verifies SSL certificates for the https URL given. SSL Verification is enabled by default in the requests library and will throw an error if the certificate is not present.

Authentication

HTTP 身份验证是在服务器端要求某些身份验证信息,例如用户名、密码,当客户端请求一个 URL 时。这是对客户端和服务器之间交换的请求和响应的附加安全措施。

HTTP authentication is on the server-side asking for some authentication information like username, password when the client requests a URL. This is an additional security for the request and the response being exchanged between the client and the server.

Advantages of using Python Requests Library

以下是使用 Python Requests 库的优点 −

Following are the advantages of using Python Requests Library −

  1. Easy to use and fetch the data from the URL given.

  2. Requests library can be used to scrape the data from the website.

  3. Using requests, you can get, post, delete, update the data for the URL given.

  4. The handling of cookies and session is very easy.

  5. The security is also taken care of the help of authentication module support.