After the environment is set up, let's look at some simple uses of requests, including:
- Requests commonly used request methods, including: Get,post
- Use of session and cookie in requests library
- Other advanced sections: Authentication, proxy, certificate validation, timeout configuration, error handling, and more.
This section begins with a look at how to send a GET request in the requests library:
First, look at the method definition:
1, to the official document went to the next requests.get () method definition, as follows:
2, click "Source" in the upper right corner, to see its source code as follows:
See the last line Return,get method is finally implemented by calling the Requests.request method, in fact, in other methods such as Post,put,head,delete request method, such as the invocation method, The type of the requested method is then passed to the request method's first parameter.
3, the HTTP protocol is a request/response mode-based, stateless, application-level protocol. Since there is a request, there is a response, to see the response information commonly used in Resquest:
Second, the Get method is simple to use:
1. Get with no parameters:
#-*-coding:utf-8-*-#get with no parametersImportRequestsImportJsonhost="http://httpbin.org/"Endpoint="Get"URL="'. Join ([Host,endpoint]) R=requests.get (URL)#response = R.json ()Printtype (r.text)Print(eval (r.text))
Output:
{ 'Origin':'183.14.133.88', 'Headers': { 'Connection':'Close', 'Host':'httpbin.org', 'accept-encoding':'gzip,Deflate', 'Accept':'*/*', 'user-agent':'python-requests/2.18.1' }, 'args': { }, 'URL':'http://httpbin.org/get'}
2. Get with parameters:
#-*-coding:utf-8-*-#Get with ParametersImportRequestsImportJsonhost="http://httpbin.org/"Endpoint="Get"URL="'. Join ([host,endpoint]) params= {"show_env":"1"}r= Requests.get (url=url,params=params)PrintR.url
Output:
Http://httpbin.org/get?show_env=1{ 'Origin':'183.14.133.88', 'Headers': { 'X-request-id':'Ebe922b4-c463-4fe9-9faf-49748d682fd7', 'accept-encoding':'gzip,Deflate', 'X-forwarded-port':' the', 'Total-route-time':'0', 'Connection':'Close', 'Connect-time':'0', 'Via':'1.1vegur', 'x-forwarded-for':'183.14.133.88', 'Accept':'*/*', 'user-agent':'python-requests/2.18.1', 'X-request-start':'1504755961007', 'Host':'httpbin.org', 'X-forwarded-proto':'http' }, 'args': { 'show_env':'1' }, 'URL':'http://httpbin.org/get?show_env=1'}
3. Get with Header:
#-*-coding:utf-8-*-ImportRequestsImportJsonhost="http://httpbin.org/"Endpoint="Get"URL="'. Join ([host,endpoint]) headers= {"user-agent":"Test Request Headers"}r=requests.get (URL) r= Requests.get (url,headers=headers)#response = R.json ()Print(eval (r.text)) ['Headers']['user-agent']
Output:
Test request Headers
4, with parameters and header:
#-*-coding:utf-8-*-ImportRequestsImportJsonhost="http://httpbin.org/"Endpoint="Get"URL="'. Join ([host,endpoint]) headers= {"user-agent":"Test Request Headers"}params= {"show_env":"1"}r=requests.get (URL) r= Requests.get (url,headers=headers,params=params)#response = R.json ()Print(eval (r.text)) ['Headers']['user-agent']PrintR.url
Output:
Test Request headershttp://httpbin.org/get?show_env=1
Python Interface Automation Test (ii)-requests.get ()