I. Introduction
python interface testing to do, we need to understand and learn third-party libraries requests. urllib python built-in module is also used to access network resources, but it is less cumbersome and lacks many useful advanced features. It is recommended to use requests
Chinese help documentation: http://cn.python-requests.org/zh_CN/latest/index.html
English Help documentation: https://2.python-requests.org//en/master/
Second, how to install requests
cmd enter the command: pip install requests
Reference Links: https://www.cnblogs.com/king8/p/9216133.html
Third, the use requests
- no get request parameter param
1. Import module requests to access a website url address through get, such as access to garden blog personal blog: https: //www.cnblogs.com/huainanhai/
2.r is the response, the request for the return value, you can call response in the status_code way to view the status code
3. Status Code 200 only shows the server address to access this interface is right, does not explain the function OK, generally view the content of the response, r.text return text message
# Import module requests Import requests # requested blog personal blog Park R & lt requests.get = ( " https://www.cnblogs.com/huainanhai/ " ) # Print Status Code Print (r.status_code) # print text Print (R & lt. text)
- There get request parameter param
1. Import module requests, arguments get access through a web url address, such as a personal blog search: Interface, url address:
2. Request parameter: q = West, can be transferred in the form of a dictionary reference: { "q": "Journey"}
3. Multiple parameter format: { "key1": "value1", "key2": "value2", "key3": "value3"}
# Import module requests Import requests # blog Park official website search parameter param = { " T " : " B ", "W": "side edges" } # request Park blog search margin of R & lt requests.get = ( " HTTPS: // zzk.cnblogs.com/s " , the params = param ) # print status code Print (r.status_code) # print URL Print (r.url) # print text Print (r.text)
PS:. 1 watercress behind the web site do not leak /search;2.params do not write param
IV, appendix response message returned
1.r.status_code # Response Status Code
2.r.url # get url
3.r.content # byte response body, and automatically decoding gzip deflate compression for you
4.r.headers # dictionary object storage server in response to the first, the dictionary is not case sensitive, if the key does not exist is returned None
5.r.encoding # coding format, requests the automatic detection coding
6.r.cookies # get cookie