GET and POST Request Patterns of python's requests Module

Keywords: encoding JSON Python Windows

python's requests module can simulate browser requests. Compared with urllib, the api of requests module is more convenient (essentially encapsulating urllib3).

Installation mode

pip install requests

GET-based requests

response = requests.request("get", "")

If you want to add headers, you can pass in the headers parameter to increase the headers information in the request header. If you want to pass parameters in the url, you can use params parameters.

import requests

kw = {'wd': 'Polar bear'}

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                  "AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/54.0.2840.99 Safari/537.36"}

# params receives the query parameters of a dictionary or string, and the dictionary type is automatically converted to url encoding without the need of urlencode().
response = requests.get("", params=kw, headers=headers)

# Looking at the response content, response.text returns data in Unicode format

# View the response content, byte stream data returned by response.content

# View the full url address

# View response header character encoding

# View the response code
  • When using response.text, Requests automatically decodes response content based on HTTP response text encoding, and most Unicode character sets can be seamlessly decoded. The result you print may be scrambled. Just call the encoding method in front and set it to'utf-8'.
  • When using response.content, the original binary byte stream of server response data is returned, which can be used to save binary files such as pictures.

POST-based requests

For post requests, we usually pass with parameters, or even with data.

import requests

formdata = {
    "type": "AUTO",
    "i": "i love python",
    "doctype": "json",
    "xmlVersion": "1.8",
    "keyfrom": "fanyi.web",
    "ue": "UTF-8",
    "action": "FY_BY_ENTER",
    "typoResult": "true"

url = ""

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) "
                  "AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/51.0.2704.103 Safari/537.36"}

response =, data=formdata, headers=headers)


# If it is a json file, it can be displayed directly




Posted by ruach on Sun, 06 Oct 2019 13:13:53 -0700