Python requests Module
Python requests is a commonly used HTTP request library that makes it easy to send HTTP requests to websites and retrieve response results.
The requests module is simpler than the urllib module.
To use requests to send HTTP requests, you need to first import the requests module:
After importing, you can send HTTP requests using the methods provided by requests to a specified URL, for example:
Example
Section titled “Example”Each time you call a requests method, it returns a response object containing specific response information such as status code, response headers, response content, etc.:
More response information is as follows:
| Property or Method | Description |
| apparent_encoding | Encoding method |
| close() | Close the connection to the server |
| content | Returns the response content in bytes |
| cookies | Returns a CookieJar object containing the cookies sent back from the server |
| elapsed | Returns a timedelta object containing the time elapsed between sending the request and receiving the response, useful for testing response speed. For example, r.elapsed.microseconds indicates how many microseconds the response took. |
| encoding | The encoding method for decoding r.text |
| headers | Returns the response headers in dictionary format |
| history | Returns a list of response objects containing the request history (urls) |
| is_permanent_redirect | Returns True if the response is a permanently redirected URL, otherwise False |
| is_redirect | Returns True if the response was redirected, otherwise False |
| iter_content() | Iterates over the response |
| iter_lines() | Iterates over the response lines |
| json() | Returns the result as a JSON object (the result must be in JSON format, otherwise an error will be raised) |
| links | Returns the parsed header links of the response |
| next | Returns the PreparedRequest object for the next request in the redirect chain |
| ok | Checks the value of “status_code”. Returns True if it is less than 400, otherwise False |
| raise_for_status() | If an error occurs, the method returns an HTTPError object |
| reason | The description of the response status, such as “Not Found” or “OK” |
| request | Returns the request object that requested this response |
| status_code | Returns the HTTP status code, such as 404 and 200 (200 is OK, 404 is Not Found) |
| text | Returns the response content as unicode type data |
| url | Returns the URL of the response |
Example
Section titled “Example”The output is as follows:
Request a JSON data file and return the JSON content:
Example
Section titled “Example”The output is as follows:
requests Methods
Section titled “requests Methods”The requests methods are as shown in the table below:
| Method | Description |
| delete(url, args) | Sends a DELETE request to the specified URL |
| get(url, params, args) | Sends a GET request to the specified URL |
| head(url, args) | Sends a HEAD request to the specified URL |
| patch(url, data, args) | Sends a PATCH request to the specified URL |
| post(url, data, json, args) | Sends a POST request to the specified URL |
| put(url, data, args) | Sends a PUT request to the specified URL |
| request(method, url, args) | Sends the specified request method to the specified URL |
Using requests.request() to send a GET request:
Example
Section titled “Example”The output is as follows:
Setting request headers:
Example
Section titled “Example”The output is as follows:
The post() method sends a POST request to the specified URL. The general format is as follows:
-
url is the request URL.
-
data parameter is the dictionary, tuple list, bytes, or file object to send to the specified URL.
-
json parameter is the JSON object to send to the specified URL.
-
args are other parameters such as cookies, headers, verify, etc.
Example
Section titled “Example”The output is as follows:
POST request with parameters:
Example
Section titled “Example”The output is as follows:
Additional Request Parameters
Section titled “Additional Request Parameters”When sending requests, we can attach additional parameters such as request headers, query parameters, request body, etc., for example:
The above code sends a POST request and attaches request headers, query parameters, and a request body.
In addition to basic GET and POST requests, requests also supports other HTTP methods such as PUT, DELETE, HEAD, OPTIONS, etc.