Python Requests Tutorial

Python Requests Tutorial

Introduction

Python's requestslibrary 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 requeststhe library to send GET and POST requests, and how to process the responses.

Install

First, make sure you have Python installed. Then, requeststhe library can be installed with the following command:

pip install requests

Send a GET request

Sending a GET request is very easy using requeststhe 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.comand save the response in responsea 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. requestsLibraries 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

requestsSending 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 payloadas a parameter.

Handle the response

responseThe object contains all the information returned by the server. The content of the response can be obtained by calling response.textthe 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, responsethe object also contains other useful properties and methods, such as response.status_codethe status code that can get the response, and response.headersthe header information that can get the response.

error handling

During the sending of the request, various errors may be encountered. requestsThe 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 requestsexceptions that may be thrown by the library and handle them accordingly.

Summarize

This tutorial explains how to use requeststhe library to send GET and POST requests, and how to handle the responses. requestsThe library is very powerful and easy to use, suitable for various HTTP request scenarios. For detailed usage, please refer to requeststhe official documentation of the library.

reference link

Guess you like

Origin blog.csdn.net/sinat_35773915/article/details/132187839