One 、 Basic introduction
1, What is? Requests library ?
(1) Requests Yes, it is python Language based urllib Compiling , It's using Apache2 Licensed Open source protocol HTTP library . (2) Requests Compared with urllib Concise and many , It can be said that at present Python The easiest to implement HTTP library .
2,Requests Library feature
- Keep-Alive & Connection pool
- International domain names and URL
- With lasting Cookie Conversation
- Browser style SSL authentication
- Automatic content decoding
- basic / Abstract authentication
- elegant key/value Cookie
- Automatically decompress
- Unicode Response body
- HTTP(S) Agent support
- Upload files in blocks
- Stream download
- Connection timeout
- Block request
- Support .netrc
3, install Requests library
(1) because Requests It's a third party library , Therefore, the following commands should be executed in the terminal before use pip install :
pip install requests
(2) Because of my Mac The computer is equipped with the latest Python 3.7 edition ( Alias python3), It comes with it pip, So execute the following command to install :
python3 -m pip install requests
Two 、 Basic usage
1, Send a request
Requests It provides us with various ways to request :
import requests
requests.get('https://api.github.com/events')
requests.post("http://httpbin.org/post")
requests.put("http://httpbin.org/put")
requests.delete("http://httpbin.org/delete")
requests.head("http://httpbin.org/get")
requests.options("http://httpbin.org/get")
2, Pass parameters
(1) If you need to pass parameters , We can splice the parameters directly into URL Back :
requests.get('http://httpbin.org/get?name=hangge.com&age=100')
(2) You can also use params Keyword passes a dictionary in as a parameter ( Parameters will also be automatically spliced to URL Back ):
import requests
data = {
"name": "hangge.com",
"age": 100
}
response = requests.post('http://httpbin.org/post', params=data)
print(response.text)
(3) If you use data Keyword passes a dictionary in as a parameter , Parameters will be passed in the form of a form ( Parameters will not be spliced to URL Back ):
import requests
data = {
"name": "hangge.com",
"age": 100
}
response = requests.post('http://httpbin.org/post', data=data)
print(response.text)
(4) Sometimes the data we want to send doesn't need to be submitted in form . Like passing on a string Type of JSON strand , have access to json Parameters to transmit :
import requests
data = {
"name": "hangge.com",
"age": 100
}
response = requests.post('http://httpbin.org/post', json=data)
print(response.text)
3、 ... and 、 In response to the results
1, Get response content
(1) When using Requests When making a network request , Will return a Response object , We can use it to get the response content :
import requests
response = requests.get("https://www.baidu.com")
print("\n--- type(response) ---\n", type(response))
print("\n--- response.status_code ---\n", response.status_code) # Response status code
print("\n--- response.cookies ---\n", response.cookies) #Cookie
print("\n--- type(response.text) ---\n", type(response.text))
print("\n--- response.text ---\n", response.text) # Response text content
print("\n--- response.content ---\n", response.content)
(2) In many cases, if you directly output response.text There will be garbled problems ( See above ), There are two ways to solve this problem :
- First, use. response.content Get the binary response content , And then through decode() Convert to utf-8.
import requests
response = requests.get("https://www.baidu.com")
print(response.content.decode("utf-8"))
- Second, it can be used response.encoding Property to specify the response encoding to use :
import requests
response = requests.get("https://www.baidu.com")
response.encoding="utf-8"
print(response.text)
Either way above , You can see that the problem of garbled code has been solved :
2,JSON Response content
(1) If the request returns JSON Formatted data , have access to Requests Built in JSON decoder , It can help us deal with JSON data :
import requests
response = requests.get('http://httpbin.org/get?name=hangge.com&age=100')
json = response.json()
print(json)
print(json['args']['name'])
(2) Of course we can use it json.loads() Method manually json The string is converted to dict Dictionaries , The effect of the following code is the same as that of the above :
import requests
import json
response = requests.get("http://httpbin.org/get?name=hangge.com&age=100")
json = json.loads(response.text)
print(json)
print(json['args']['name'])