Introduction to the
If we want to do interface testing in Python, we first have modules that we have to understand and learn. This is the third-party module: Requests. Although Python has a built-in urllib module for accessing network resources. However, it is cumbersome to use and lacks many useful advanced features. A better solution is to use
Requests. It is a Python third-party library that is particularly handy for handling URL resources. Check out its Official Chinese website: cn.python-requests.org/zh_CN/lates… English official website: www.python-requests.org/en/master/ you can take a look at its many practical advanced features.
features
Requests fully meets the needs of today’s Web.
- Keep-alive & connection pool
- Internationalized domain name and URL
- Session with persistent cookies
- Browser-based SSL authentication
- Automatic content decoding
- Basic/digest authentication
- Elegant key/value cookies
- Automatically decompressed
- Unicode response body
- HTTP(S) proxy support
- The file is uploaded in chunks
- Flow to download
- Connection timeout
- Block the request
- support
.netrc
Environment Install requests
If you have Anaconda installed, Requests is already available. Otherwise, install via PIP from the command line:
pip install requestsCopy the code
Note: PIP is easily upgraded,
Method 1: Test and update
PIP list - outdatedCopy the code
Method 2:
Uninstall first:
pip uninstall packagenameCopy the code
♦ Use: easy_install.exe for installation
easy_install.exe pipCopy the code
Installing collected packages: IDNA, Chardet, Certifi, Requests
Successfully installed Certifi-2019.3.9 chardet-3.0.4idNa-2.8 requests-2.21.0 Indicates that the requests module was Successfully installed!
Using Requests (get requests without param)
1. After importing the installed Requests module, use GET to access a web page with a URL, such as https://www/douban.com
2. R here is response, the return value after the request. You can call the status_code method in response to check the status code
Status code 200 only indicates that the interface is accessing the correct server address, but does not indicate that the function is OK
See! Here’s the power of requests, doesn’t it look awesome:
Using Requests (get requests with param)
1, again hair a get request with parameters, such as in douban search: journey to the west, the url is: https://www.douban.com/search?q=, journey to the west
2, request parameters: q= journey to the West, can be in the form of dictionary :{“q”: “Journey to the West “}
3, multiple parameters format: {” key1 “:” value1 “, “key2” : “value2”, “key3” : “value3”}
This part of the most likely to make mistakes, is 1, douban website after no +/search; 2. Params is incorrectly written as param
Use request (get to get the response text content)
1. If r.ext is used on the homepage of Douban.com, garbled characters will be found, because the homepage response content of Douban.com is gzip compressed (not text).
2. If you’re using Fiddler, you can decode it by clicking on it. Using r.Tent, content will automatically decode Gzip and Deflate compression.
Appendix Response Other information returned
1. There is more information about the content of response
— r.tatus_code # Response status code
— r.tent # byte response body will automatically decode gZIP and Deflate compression for you
— r.haeaders # Store the server response headers as dictionary objects, but this dictionary is special. The dictionary key is case insensitive and None is returned if the key does not exist
— R.json () #Requests is a built-in JSON decoder for Requests. Requests is also convenient in that specific types of responses, such as JSON, can be obtained directly
— r.url # fetch the URL
— R.encoding # encoding format, requests automatically detect encoding
— r.cookies # get cookies
— r.raw # returns the raw response body — r.ext # the string response body is automatically decoded according to the character encoding in the response header
— r.aise_for_status () # Failed request (non-200 response) throws an exception
summary
Interface for sending GET requests with Requests, that’s it! If you want to learn something, go ahead and start typing. You can use your own blog garden to practice.
Python Interface Automation Communication group: 984942724
Your affirmation is my motivation for progress. If you feel good, please encourage! Remember to point wave recommendation oh!! (Click on the ball on the right! (^__^) Hee hee…
Personal public account
Wechat group (the wechat group has reached 100, you can add Hongge’s wechat to pull you into the group, please note: enter the group)