introduction

In network requests, the requests library in Python is a very powerful and popular library. It provides a simple, elegant and flexible way to send HTTP requests and handle responses. This article will introduce the functions and usage of the requests library in detail to help readers understand how to use this library to implement various network requests.

What is requests library

requests is a Python third-party library for sending HTTP requests. It is encapsulated based on Python’s urllib library, providing a higher-level API and more convenient usage.

The following functions can be completed using the requests library:

  • Send various types of requests such as GET, POST, PUT, DELETE, etc.
  • Set parameters such as Headers, Cookies, and User-Agent of the request.
  • Process the response status code, headers, content and other information.
  • Use Session object for session management.
  • Upload files and get files.
  • Handles redirects and HTTPS requests.
  • Used in conjunction with other third-party libraries (such as JSON parsing library, BeautifulSoup, etc.).

Install requests library

Before using the requests library, you need to install it. You can use the pip command to install:
1

Send GET request

Sending a GET request is one of the most common request types. Sending GET requests is very simple using the requests library. Here is a sample code that sends a GET request and gets the response content:
2

Send POST request

Sending a POST request is typically used to submit data to the server. Likewise, sending POST requests is very simple using the requests library. Here is a sample code that sends a POST request and gets the response content:
3

Request parameters

When sending a request, parameters can be passed via query string or request body parameters. The requests library provides multiple ways to pass parameters.

Query string parameters

Query string parameters are parameters added after the ? of the URL. Query string parameters can be passed to the requests.get method using the params parameter. Here is a sample code that sends a GET request with query string parameters:
4

In the code above, we define a URL and a dictionary containing keywords and pages. Then a GET request with query string parameters was sent using the requests.get method. Finally, get the full URL via the response.url property and print it out.

JSON parameters

When sending a request, sometimes you need to pass parameters to the server in JSON format. JSON parameters can be passed to the requests.post method using the json parameter. Here is a sample code that sends a POST request with JSON parameters:
5
In the code above, we define a URL and a dictionary containing names and ages. Then a POST request with JSON parameters was sent using the requests.post method. Finally, get the response content through the response.text property and print it out.

RequestHeaders

When sending a request, you can set the header parameters of the request. The requests library provides a variety of ways to set request Headers parameters.

Set request headers

You can use the headers parameter to set the header parameters of the request. Here is an example code for setting request headers:
6
In the above code, we first define a URL and a dictionary containing User-Agent. Then a GET request with request headers was sent using the requests.get method. Finally, get the response content through the response.text property and print it out.

Automatically set Referer header

The Referer header can be set using the Referer parameter. The following is a sample code that automatically sets the Referer header:
7
In the above code, we define a URL and a Referer. Then a GET request with a Referer header was sent using the requests.get method. Finally, get the response content through the response.text property and print it out.

File upload and download

Files can be downloaded using the stream=True parameter. Here is a sample code for downloading a file:
8
In the above code, we define a download URL and send a GET request using the requests.get method, and set the stream=True parameter to stream the file. Then write the response content to the file block by block through the write() method of the file object. Finally, print out the prompt message that the download is complete.

Conclusion

So far, we have introduced the requests library in detail and discussed its common functions and usage. It is hoped that through the introduction of this article, readers can fully understand and master the use of the requests library, and can flexibly apply it to network request scenarios in actual development. If you want to learn more about the requests library, it is recommended to consult the official documentation and other related materials.