preface

The text and pictures in this article come from the network, only for learning, communication, do not have any commercial purposes, if you have any questions, please contact us to deal with.

The following article is from APython, written by Aluminum

! [](https://p26-tt.byteimg.com/img/pgc-image/da28b95cca4649339394e86737fbf157~tplv-tt-shrink:640:0.image)

1. Introduction of Requests

Requests are the only Non-GMO HTTP library for Python that is safe for human use.

One thing Python crawlers can’t bypass is the Requests library. The Requests reference URllib is much more user-friendly, concise, and comfortable to use. The following features are extracted from the Requests official documentation:

  1. Keep alive and connect pools
  2. Internationalized domain name and URL
  3. Session with permanent Cookie
  4. Browser-based SSL authentication
  5. Automatic content decoding
  6. Basic/digest authentication
  7. Elegant key/value cookies
  8. Automatically decompressed
  9. Unicode response body
  10. HTTP (S) proxy support
  11. The file is uploaded in chunks
  12. Flow to download
  13. Connection timeout
  14. Block the request
  15. Support. Netrc

2. Installation requirements

Requests are python’s tripartite library, so we need to use PIP installation

pip install requests
Copy the code

Or through binary installation

CD < git clone git://github.com/kennethreitz/requests.git requests directory > python setup. Py installCopy the code

3. Request use cases

Common HTTP operations are GET and POST. For other operations, refer to official documents or call corresponding methods through serial ports.
GET ("https://getman.cn/echo") print(response.text) # GET Headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36', 'Accept ': 'text/html,application/xhtml+xml,application/xml; Q = 0.9, image/avif, image/webp image/apng, * / *; Q = 0.8, application/signed - exchange; v=b3; Q = 0.9 ', } cookie = {"user":"APython"} params = {'my_name':'AL','name':'APython'} response=requests.get("https://getman.cn/echo",headers=headers,cookies=cookie,params=params) print(response.text) #POST Request data = {'name': 'apython-post ','age': 24,} response = requests.post("https://getman.cn/echo", data=data) print(response.text)Copy the code
! [](https://p9-tt-ipv6.byteimg.com/img/pgc-image/4438b333bc284e35b6d827f81f7320e7~tplv-tt-shrink:640:0.image)
The return value response is a Response object, and its common properties and methods are as follows:
! [](https://p1-tt-ipv6.byteimg.com/img/pgc-image/6c04770cb5684aaea309cc12b9456e05~tplv-tt-shrink:640:0.image)

4. Request more examples

The import requests # download file (a) small file url = 'https://raw.githubusercontent.com/psf/requests/master/ext/ss.png' response = requests.get(url) with open('demo.png', 'wb') as f: F.w rite (response. The content) # download file (2) large files file_url = "https://readthedocs.org/projects/python-guide/downloads/pdf/latest/" response = requests.get(file_url) with open("python.dpf", "wb") as pdf: for chunk in response.iter_content(chunk_size=1024): if chunk: PDF. Write (the chunk) # POST submission data returns the url = 'https://api.github.com/some/endpoint' data = {' some ': 'APython'} response = requests.post(url, Data =data) print(response.text) #session = requests. Session () session.get(url) session.post(url,data)Copy the code