Detailed explanation of Python requests module

  • 2021-11-13 02:19:09
  • OfStack

Preface

Although the urllib2 module in the standard library of Python already contains most of the functions we usually use, its API doesn't feel very good to use, while Requests calls itself "HTTP for Humans", which shows that it is simpler and more convenient to use.

Requests inherits all the features of urllib2. Requests supports HTTP connection retention and connection pooling, supports session retention using cookie, supports file uploading, supports automatic encoding of response content, and supports international automatic encoding of URL and POST data.

Open source address: https://github.com/kennethreitz/requests

Chinese document API: http://docs.python-requests.org/zh _ CN/latest/index. html

1. GET request

1.1 Basic GET requests


#  Writing style 1:
response = requests.get("http://www.baidu.com/")
#  Writing style 2:
# response = requests.request("get", http://www.baidu.com/)

1.2 Add headers and query parameters

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


import requests
 
kw = {'wd':' Great Wall '}
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  Receive 1 A dictionary or string query parameter, and the dictionary type is automatically converted to url Coding, not required urlencode()
response = requests.get("http://www.baidu.com/s?", params = kw, headers = headers)

# View the response content, response.text  Returns the Unicode Format data 
print response.text
#<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer> .....

#  View the response content, response.content Returned byte stream data 
print respones.content

#  View complete url Address 
print response.url
# http://www.baidu.com/?wd=%E9%95%BF%E5%9F%8E

#  View response header character encoding 
print response.encoding
# ISO-8859-1 

#  View the response code 
print response.status_code
# 200

2. POST request

2.1 The most basic POST request


response = requests.post("http://www.baidu.com/", data = data)

2.2 Incoming data data

For the POST request, we generally need to add 1 parameter to it. Then the most basic parameter transmission method can use the parameter 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 = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null"
 
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 = requests.post(url, data = formdata, headers = headers)

print response.text
# {"type":"EN2ZH_CN","errorCode":0,"elapsedTime":2,"translateResult":[[{"src":"i love python","tgt":" I like it python"}]],"smartResult":{"type":1,"entries":[""," Si Wen "," Gartner "]}}

import requests
 
kw = {'wd':' Great Wall '}
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  Receive 1 A dictionary or string query parameter, and the dictionary type is automatically converted to url Coding, not required urlencode()
response = requests.get("http://www.baidu.com/s?", params = kw, headers = headers)
0

Related articles: