Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Requests is an HTTP client library for Python based on the URllib standard library, which is highly encapsulated and therefore easier to use.

The Requests library implements most of the functionality of the HTTP protocol, including keep-alive, connection pooling, Cookie persistence, HTTP(S) proxy support, connection timeouts, and most importantly, Python2 and YTHon3, and runs perfectly under PyPy.

Install using the PIP install requests command before using.

Simple to use

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

The result is 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"
}
Copy the code

In addition, there are many types of HTTP requests, such as POST, PUT, DELETE, HEAD, and OPTIONS. Requests can also be implemented in a simple manner.

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")
Copy the code

From this point of view, using the Requests library is simple and convenient.

Build request query parameters

Many requests require parameters to be passed in the URL. We can use dictionaries to build query parameters and params parameters to add parameters to the URL.

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

The running results are as follows:

https://www.baidu.com/?wd=test
Copy the code

Build the request Headers

Requests can simply specify Headers information in a request, passing a dictionary to the Headers parameter.

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

Build the POST request data

Requests makes it very easy to build the data required for POST requests. If the server receives form data, it can use the data parameter. If the server receives JSON data, it can use the JSON parameter parameter.

The form data

import requests

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

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"
}
Copy the code

The 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)
Copy the code

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"
}
Copy the code

Get response content

It is also convenient and flexible to handle the response body using Requests requests, with properties such as Content, Text, and json().

The Content property gets byte data.

import requests


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

The text property gets data of type STR.

import requests


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

It is possible to use the json() method to return a json.loads() if the content returned is jSON-formatted data.

import requests

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

Cookies

If the response contains cookie information, we can retrieve it using the cookies property.

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

You can also use the cookies parameter to send cookies to the server.

url = "http://httpbin.org/cookies"
cookies = {"cookies": "xxxxx"}
r = requests.get(url, cookies=cookies)
print(r.text)
Copy the code

The timeout configuration

You can use the timeout parameter to configure the maximum request time.

requests.get("https://baidu.com", timeout=0.01)
Copy the code

The agent

If proxies are required, they can be configured using the Proxies parameter.

import requests

proxies = {
  'http': 'http://175.7.199.202:3256'.'https': 'http://175.7.199.59:3256',
}

requests.get('http://httpbin.org/get', proxies=proxies)
Copy the code

conclusion

For more information, please refer to the Requests library documentation. For more information, please refer to the Requests library documentation

Original is not easy, if small partners feel helpful, please click a “like” and then go ~

Finally, thank my girlfriend for her tolerance, understanding and support in work and life!