[Python] This article teaches you how to use the Requests library

 

Author's homepage: boy who loves to laugh. Blog_CSDN blog - deep learning, activities, python field blogger loves to laugh boy. A boy who is good at deep learning, activities, python, etc., and loves to laugh. Focus on algorithms, python, computer vision, image processing, deep learning, pytorch, neural network, opencv fields. https://blog.csdn.net/Code_and516?type=blog Personal profile: Dagong.

Continue to share: machine learning, deep learning, python-related content, daily BUG solutions, and Windows&Linux practical tips.

If you find an error in the article, please point it out, and I will correct it in time. If you have other needs, you can private message me or send me an email: [email protected] 

 

Table of contents

Introduction

History

Key features of the Requests library include:

        1. Send an HTTP request

        2. Processing the response result

        3. Session management

        4. File upload and download

        5. Other features

Steps for usage

        1. Install the Requests library

        2. Send HTTP request

        3. Processing the response result

        4. Session management

        5. File upload and download

Here is more sample code using the Requests library:

        1. Use the POST method to send an HTTP request, and pass URL parameters and request headers:

        2. Automatically process the response results in JSON format:

        3. Use a proxy server:

        4. Timeout setting:

 Summarize


Introduction

        The Requests library is a simple and easy-to-use Python HTTP library based on the urllib3 library, which can be used to send HTTP requests and process the response results. The Requests library provides a more user-friendly interface, making it easier for users to write HTTP request code. Compared with the urllib/urllib2 library that comes with Python, Requests is more complete and easy to use, and supports multiple protocols and authentication methods, with better scalability and readability.

History

        The Requests library was developed by Kenneth Reitz. Kenneth is an active member of the Python community. In 2011 he published a blog titled "Python HTTP: When in doubt, or when not in doubt, use Requests", which introduced the Requests library he developed, and The advantages of the Requests library, such as ease of use, friendliness, and scalability, are emphasized. Since then, the Requests library has become one of the most popular HTTP libraries in the Python community. The current version of the Requests library is 2.26.0, which is a stable, full-featured HTTP library.

Key features of the Requests library include:

        1. Send an HTTP request

        The Requests library can send HTTP requests, including common HTTP request methods such as GET, POST, PUT, PATCH, and DELETE, and also supports customizing request headers, request parameters, request bodies, cookies, and other information. At the same time, the Requests library also supports features such as HTTPS and proxy servers.

        2. Processing the response result

        The Requests library can process HTTP response results, including obtaining response headers, status codes, response bodies, cookies, and other information. At the same time, the Requests library also supports encoding, decoding response results, and automatically parsing response results in JSON and XML formats.

        3. Session management

        The Requests library supports Session management, allowing users to share information such as cookies and user authentication among multiple requests. At the same time, Session also supports features such as local storage, proxy and SSL verification.

        4. File upload and download

        The Requests library also supports uploading and downloading files, can handle binary streams and text streams of files, and also supports features such as breakpoint resumes.

        5. Other features

        The Requests library also supports features such as redirection processing, authentication, proxy, timeout control, and SSL verification, allowing users to better control the behavior of HTTP requests.

Steps for usage

        1. Install the Requests library

        The Requests library can be easily installed using the pip tool.

pip install requests

        If it is Linux and the pip tool is not installed, please use the following command to install it first: 

sudo apt-get install python-pip

        2. Send HTTP request

         Sending HTTP requests is easy using the Requests library. Here is a simple code:

import requests

response = requests.get('https://www.baidu.com')
print(response.text)

        In the above code, we use the requests library to send an HTTP GET request, obtain the HTML content of Baidu's homepage, and print out the text content of the response result. Of course, in addition to GET requests, request methods such as POST, PUT, PATCH, and DELETE can also be sent, as well as information such as custom request headers, request parameters, and request bodies.

        3. Processing the response result

        Handling HTTP response results is also very simple. Here is a simple code:

import requests

response = requests.get('https://www.baidu.com')
print(response.status_code)
print(response.headers)
print(response.cookies)
print(response.text)

        In the above code, we use the requests library to send an HTTP GET request, and get the status code, response header, cookies and text content of the response result. The Requests library also supports getting binary content of response results, results in JSON and XML formats, and more. At the same time, custom parsers are also supported to handle non-standard response formats. 

        4. Session management

        Using the Session management of the Requests library, information such as cookies and user authentication can be shared among multiple requests. The following is a simple session management code:

import requests

s = requests.Session()

s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get('http://httpbin.org/cookies')
print(r.text)

        In the above code, we create a Session object s, automatically handle cookies, and send two GET requests. The first request sets the cookie value, and the second request gets the current cookie value. In the second request, the Session object s is used, which automatically carries the Cookie value set in the first request. 

        5. File upload and download

        Use the Requests library to easily upload and download files. The following is a simple file upload code:

import requests

url = 'http://httpbin.org/post'
files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=files)
print(r.text)

        In the above code, we use the Requests library to send an HTTP POST request, upload a file named report.xls, and print out the text content of the response result.

Here is more sample code using the Requests library:

        1. Use the POST method to send an HTTP request, and pass URL parameters and request headers:

import requests

url = 'http://httpbin.org/post'
payload = {'key1': 'value1', 'key2': 'value2'}
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.post(url, data=payload, headers=headers)
print(response.text)

        2. Automatically process the response results in JSON format:

import requests

url = 'https://jsonplaceholder.typicode.com/todos/1'
response = requests.get(url)
data = response.json()
print(data)

        3. Use a proxy server:

import requests

proxies = {
  'http': 'http://localhost:8080',
  'https': 'https://localhost:8080',
}
response = requests.get('http://httpbin.org/ip', proxies=proxies)
print(response.text)

        4. Timeout setting:

import requests

response = requests.get('http://github.com', timeout=1)
print(response.text)

 Summarize

        The Requests library is a powerful, easy-to-use, and widely supported Python HTTP library. It has become one of the most popular HTTP libraries in the Python community for its ease of use, friendliness, and scalability. Requests provides a wealth of functions, including sending HTTP requests, processing response results, Session management, file upload and download, proxy server, redirection processing, authentication, timeout control, SSL verification, etc., and provides a clear and simple API for It is easier for users to understand and use. The Requests library is an integral part when developing Python web applications, making code clearer, more concise, and easier to maintain.

 

Guess you like

Origin blog.csdn.net/Code_and516/article/details/131103888