A simple introduction
The Requests library is an easy-to-use HTTP library
A Get request
Format: requests. Get (url)
Note: If you need to pass the request parameters, you can directly pass the? You can also call get() with an extra parameter params, passing in the request parameter in dict format. As shown in the figure below
1 url = 'http://127.0.0.1:8888/passport/user/login'
2 param = {
3 'username': '123',
4 'password': '321'
5 }
6
7 """通过params传参"""
8 res = requests.get(url, params=param)
9 # {'code': 200, 'msg': 'success', 'password': '321', 'username': '123'}
10 print(res.json())
11
12 """通过params方式传参,最终发出的url也是一致的"""
13 # http://127.0.0.1:8888/passport/user/login?username=123&password=321
14 print(res.url)
15
16 """通过url最后加上请求参数列表"""
17 url = 'http://127.0.0.1:8888/passport/user/login?username=123&password=321'
18 res = requests.get(url, verify=False)
19 # {'code': 200, 'msg': 'success', 'password': '321', 'username': '123'}
20 print(res.text)
Copy the code
Important: The RES that sends the request assignment can be thought of as the context of the request, and can be used to retrieve both request-related and response-related parameter values
A Post request
Format: requests. Post (url, data)
Note: If there is no request parameter, do not pass data; When the request parameters are passed in, the dict format is required
2 data = {1 url = 'http://127.0.0.1:8888/passport/user/post_login' 3 'username' : '123', 4 'password' : 7 res = requests. Post (url, data=data) 9 # {'code': 200, 'MSG ': 'success', 'password': '321', 'username': '123'} 10 print (res) json ()) 11 12 "to view the request url" "" "" 13 # http://127.0.0.1:8888/passport/user/post_login 14 print (res) url)Copy the code
Other Types of requests
1 r = requests.put('http://httpbin.org/put', data={'key': 'value'}).text
2 r = requests.delete('http://httpbin.org/delete').text
3 r = requests.head('http://httpbin.org/head').text
Copy the code
Customize headers and cookies
Custom headers "" "" 1 "" 2 url = 'https://api.github.com/some/endpoint' 3 headers = {' the user-agent: 1 r = requests. Get (url, headers=headers) 2 # {'message': 'Not Found', 'documentation_URL ': 'https://developer.github.com/v3'} 7 print (r.j son (), 8 and 9 "custom cookies" "" "" "10 url = 'http://httpbin.org/cookies' 11 cookies = dict(cookies_are='working') 12 cookies2 = {'cookies_are': 'working'} 13 14 r = requests.get(url, cookies=cookies) 15 # {'cookies': {'cookies_are': 'working'}} 16 print(r.json())Copy the code
SSL Certificate Verification
Verify =False; verify=False; verify=False; This is done to avoid the validation step
url = 'https://www.imooc.com'
res = requests.get(url, verify=False)
Copy the code
Response content
Note: When calling json(), make sure the response content is a JSON-formatted string, otherwise an error will be reported
1 url = 'http://127.0.0.1:8888/passport/user/login' param = {2 3 'username' : '123', 4 'password' : 5} 6 res = requests. Get (url, params=param) 7 print(' request url: '+ res.url) 8 print(' request url:') '+ json.dumps(res.json())) 9 print(' dumps' string format:' + res.text) Print (' STR (res.status_code) ')Copy the code
Access to the header
Note that the key in headers is case insensitive
>>> r.headers['Content-Type']
'application/json'
>>> r.headers.get('content-type')
'application/json'
Copy the code
To get the cookies
>>> r.cookies['example_cookie_name']
'example_cookie_value'
Copy the code