Python HTTP Library requests Simple Use Details

  • 2021-12-04 19:11:14
  • OfStack

Directory 1, Simple Use 2, Build Request Query Parameter 3, Build Request Header Headers4, Build POST Request Data 4.1 Form Data4.2 json Data5, Get Response Content 6, Cookies7, Timeout Configuration 8, Agent

requests The library implements most of the functions in HTTP protocol and provides Keep-Alive Connection pooling, Cookie Persistence, HTTP (S) proxy support, connection timeout and many other features, most importantly, it also supports Python2 And ython3, and can run perfectly under PyPy.

Need to use before using pip install requests Command to install.

1. Easy to use


res = requests.get("http://httpbin.org/get")
#  Status code 
print(res.status_code)
#  Response header 
print(res.headers["Content-Type"], res.headers["Server"])
#  Response content 
print(res.text)

The implementation results are as follows:

200
application/json gunicorn/19.9.0
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
.......
},
"origin": "xxx.xxx.xx.xx",
"url": http://httpbin.org/get
}

In addition, http There are many other types of requests, such as POST, PUT, DELETE, HEAD, OPTIONS. requests can also be implemented in a simple way.


res = requests.post("http://httpbin.org/post")
res = requests.put("http://httpbin.org/put")
res = requests.delete("http://httpbin.org/delete")
res = requests.head("http://httpbin.org/get")
res = requests.options("http://httpbin.org/get")

From this point of view, using requests Library is really simple and convenient.

2. Build request query parameters

Many requests need to pass parameters in URL. We can use dictionaries to build query parameters and use params parameters to add parameters in URL.


payload = {"wd": "test"}
res = requests.get("https://www.baidu.com/", params=payload)
print(res.url)

The running results are as follows:

https://www.baidu.com/?wd=test

3. Build the request header Headers

requests You can simply specify the request header in the request Headers Information, directly pass a dictionary to the parameter headers That's enough.


headers = {"user-agent": "Mozilla/5.0", "cookies": "xxx"}
res = requests.get("https://www.baidu.com/", headers=headers)

4. Build POST request data

requests It is very convenient to build the data required by POST request. If the data received by the server is form data, you can use the parameter data to upload, and if the data received is json format, you can use json Parameters are sent up.

4.1 Form Data


import requests

data = {"key1": "value1", "key2": "value2"}
res = requests.post("http://httpbin.org/post", data=data)
print(res.text)

The running results are as follows:

{
"args": {},
"data": "",
"files": {},
"form": {
"key1": "value1",
"key2": "value2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "23",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.26.0",
"X-Amzn-Trace-Id": "Root=1-614d7d91-559333ee19237f845026ef37"
},
"json": null,
"origin": "xxx.xxx.xx.xx",
"url": "http://httpbin.org/post"
}

4.2 json data


import json
import requests

url = "http://httpbin.org/post"
data = {"key": "value"}
data = json.dumps(data)
res = requests.post(url, data=data)
print(res.text)

The running results are as follows:

{
"args": {},
"data": "{\"key\": \"value\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "16",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.26.0",
"X-Amzn-Trace-Id": "Root=1-614d7e91-065887f925dce94d6d03b2e4"
},
"json": {
"key": "value"
},
"origin": "xxx.xxx.xx.xx",
"url": "http://httpbin.org/post"
}

5. Get the response content

Use requests The request processing response body is also very convenient and flexible, and the attributes you can use are content , text , json() .

content Property gets the byte Data of type.


import requests


res = requests.get("http://httpbin.org/get")
print(res.content)

The text property gets data of type str.


import requests


res = requests.get("http://httpbin.org/get")
print(res.text)

If the returned content is json You can use the json () method to return an object processed by json. loads ().


import requests

url = "http://httpbin.org/post"
res = requests.post(url)
print(res.json())

6. Cookies

If the response contains cookie Information that we can use cookies Property is obtained.


res = requests.get("http://httpbin.org/get")
print(res.cookies)

You can also use the cookies Parameter is sent to the server cookies Information.


res = requests.post("http://httpbin.org/post")
res = requests.put("http://httpbin.org/put")
res = requests.delete("http://httpbin.org/delete")
res = requests.head("http://httpbin.org/get")
res = requests.options("http://httpbin.org/get")

0

7. Timeout configuration

Available timeout Parameter to configure the maximum request time.


res = requests.post("http://httpbin.org/post")
res = requests.put("http://httpbin.org/put")
res = requests.delete("http://httpbin.org/delete")
res = requests.head("http://httpbin.org/get")
res = requests.options("http://httpbin.org/get")

1

8. Agent

If we need to use a proxy, we can use it through proxies Parameter to configure.


res = requests.post("http://httpbin.org/post")
res = requests.put("http://httpbin.org/put")
res = requests.delete("http://httpbin.org/delete")
res = requests.head("http://httpbin.org/get")
res = requests.options("http://httpbin.org/get")

2

Summary:


Related articles: