Python Requests Tutorial
Introduction
Python's requests
library is a simple and easy-to-use HTTP request library, which can easily send HTTP requests and process responses. This tutorial will show how to use requests
the library to send GET and POST requests, and how to process the responses.
Install
First, make sure you have Python installed. Then, requests
the library can be installed with the following command:
pip install requests
Send a GET request
Sending a GET request is very easy using requests
the library. Just call requests.get()
the method and pass in the URL as a parameter.
import requests
response = requests.get('http://www.example.com')
The above code will send a GET request to http://www.example.com
and save the response in response
a variable.
Send a GET request with parameters
If you need to send a GET request with parameters, you can add query string parameters to the URL. requests
Libraries can be implemented by get()
passing parameters in methods params
.
import requests
payload = {
'key1': 'value1', 'key2': 'value2'}
response = requests.get('http://www.example.com', params=payload)
The above code will send a GET request with query string parameters to http://www.example.com
.
Send a POST request
requests
Sending a POST request is also very simple using the library. Just call requests.post()
the method and pass in the URL and request body as parameters.
import requests
payload = {
'key1': 'value1', 'key2': 'value2'}
response = requests.post('http://www.example.com', data=payload)
The above code will send a POST request to http://www.example.com
, with the request body payload
as a parameter.
Handle the response
response
The object contains all the information returned by the server. The content of the response can be obtained by calling response.text
the method.
import requests
response = requests.get('http://www.example.com')
print(response.text)
The above code will print out the response content returned by the server.
In addition, response
the object also contains other useful properties and methods, such as response.status_code
the status code that can get the response, and response.headers
the header information that can get the response.
error handling
During the sending of the request, various errors may be encountered. requests
The library provides methods to handle these errors.
import requests
try:
response = requests.get('http://www.example.com')
response.raise_for_status() # 如果响应状态码不是200,将抛出异常
except requests.exceptions.HTTPError as err:
print('HTTP Error:', err)
except requests.exceptions.RequestException as err:
print('Error:', err)
The above code will catch requests
exceptions that may be thrown by the library and handle them accordingly.
Summarize
This tutorial explains how to use requests
the library to send GET and POST requests, and how to handle the responses. requests
The library is very powerful and easy to use, suitable for various HTTP request scenarios. For detailed usage, please refer to requests
the official documentation of the library.