The official document:
Installation: pip install request
Features: requests greatest feature is its style is simple and direct and elegant.
1. Method Request
import requests # Import Requests resp_1 = Requests. get ( ' http://httpbin.org/post ' ) # get request transmitted resp_2 = requests.post ( ' http://httpbin.org/post ' , {Data = ' Key ' : ' value ' }) # sending post request, data: transmission parameters resp_3 = requests.put ( ' http://httpbin.org/put ' , {Data = ' Key ' : ' value ' }) put request sent # , data: transmission parameters resp_4 = resp_5 = requests.head ( 'http://httpbin.org/get ' ) # head transmission request resp_5 = requests.options ( ' http://httpbin.org/get ' ) # request options sent resp_6 = requests.delete ( ' HTTP: // httpbin. ORG / delete ' ) # delete request sent
2. URL parameters passed
# Requests transmitted using the keyword the URL parameter params Import Requests params = { ' key1 ' : ' VALUE1 ' , ' key2 ' : [ ' value2 ' , ' value3 ' ]} RESP = Requests. GET ( ' HTTP: // httpbin. ORG / GET ' , the params = the params ) Print (resp.url)
3. Custom header
import requests url = 'https://www.douban.com/' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6821.400 QQBrowser/10.3.3040.400' } resp = requests.get(url=url,headers=headers) resp.encoding = 'utf-8' print(resp.text)
4. Custom Cookie
import requests cookies = { 'cookies_are': 'working' } resp = requests.get('http://www.baidu.com',cookies=cookies) for items in resp.cookies.items(): cookies[items[0]] = items[1] print(cookies)
5. Set Agent
proxies = { 'http': 'http://127.0.0.10:3126', 'https': 'http://10.10.1.10:1080' } requests.get('http://example.org',proxies=proxies)
6. Redirection
# A network request, we often encounter is to redirect status code beginning with Question 3, # in Requests is enabled by default to allow redirection, that is encountered when redirected automatically continue to visit. # Used to control whether to turn redirects allow_redirects RESP = requests.get ( ' http://github.com ' , allow_redirects = False) Print (resp.status_code)
7. prohibit certificate validation
" Sometimes we use a packet capture tool, this time due to the capture certificate tools provided by the digital certificate is not a trusted certificate authority, " " so the certificate verification fails, so we need to turn off certificate validation. In when the request to verify the parameter set to False certificate validation can be closed up. " RESP = requests.get ( ' http://httpbin.org/post ' , verify = False)
8. Set timeout
" Set Access Timeout, can set the timeout parameter " requests.get ( ' http://github.com ' , timeout = from 0.001)
The receiving response
" By Requests initiate a request to obtain, is a requests.models.Response object. Through this subject we can easily obtain the content of the response. " " Response before the adoption urllib acquired contents are read bytes of binary format , we need to own the result decode () is converted into a data string. " " and Requests by the text attribute, the content may be obtained in response to string format. " RESP = requests.get ( ' http://www.baidu .com ' ) resp.encoding = ' UTF-. 8 ' # -------------- decoding the encoding format specified Print (resp.text) # ---------- ---- string format acquiring response content Print (resp.content) # -------------- obtain the original binary data resp.json () #-------------- json format data into the data dictionary format Print (resp.status_code) # -------------- get the status code Print (resp.headers) # -------------- acquisition response header Print (resp.cookies) # -------------- acquired server returns Cookies Print (resp.url) # -------------- view visit URL
10. Case: Using complete the login request