基础使用get、post请求

常用get请求范例

  1. import requests
  2. sess = requests.Session()
  3. headers = {'Accept': 'application/json, text/javascript, */*; q=0.01',
  4. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
  5. 'Chrome/81.0.4044.122 Safari/537.36',
  6. 'Connection': 'keep-alive',
  7. 'Content-Type': 'application/json'}
  8. sess.headers = headers
  9. params = None
  10. resp = sess.get('http://www.baidu.com', params=params, verify=False)
  11. print(resp.text)

常用post请求范例

  1. import requests
  2. sess = requests.Session()
  3. headers = {'Accept': 'application/json, text/javascript, */*; q=0.01',
  4. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
  5. 'Chrome/81.0.4044.122 Safari/537.36',
  6. 'Connection': 'keep-alive',
  7. 'Content-Type': 'application/json'}
  8. sess.headers = headers
  9. data = None
  10. jsondata = None
  11. resp = sess.post('http://www.baidu.com', data=data, json=jsondata verify=False)
  12. print(resp.text)

headers请求头

headers是以dict传递的,一般使用较多的是场景是headers中添加cookie调用api,如下:

  1. headers = {'Accept': 'application/json, text/javascript, */*; q=0.01',
  2. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
  3. 'Chrome/81.0.4044.122 Safari/537.36',
  4. 'Connection': 'keep-alive',
  5. # 'connection':'close',
  6. 'Content-Type': 'application/json',
  7. 'Cookie': 'rp=7f8d3933-aff3-42b8-b9ba-e0211716renderer.sid=s%3A76xyVefkHimvKH-}

获取请求头头部信息:r.request.headers
获取响应头部信息:r.headers

get参数params

当get请求连接需要带上参数,一般使用params,以字典的形式传入。

  1. params= {'fieldType': 'CUSTOM', 'pageNo': 1, 'pageSize': 10}
  2. r = sess.get('https://home.test', params=params, verify=False)

post参数data、json

当post请求需要带上html表单参数时,可用data参数,以字典的形式传入;
当post请求需要带上json格式参数时,可以json参数,可将字典形参数自动编码。

  1. # 范例一:以web表单形式传入
  2. jsondata = None
  3. postdata = {'fieldName': 't_1', 'fieldKey': 'c_t1', 'dataType': 'TEXT',
  4. 'fieldDesc': 's', 'optiontalFalg': 0, 'optiontalItems': '', 'events': []}
  5. resp = sess.post(url, verify=False, json=jsondata, data=postdata)
  6. #范例二:以json格式形式传入
  7. postdata = None
  8. jsondata = {'fieldName': 't_1', 'fieldKey': 'c_t1', 'dataType': 'TEXT',
  9. 'fieldDesc': 's', 'optiontalFalg': 0, 'optiontalItems': '', 'events': []}
  10. resp = sess.post(url, verify=False, json=jsondata, data=postdata)
  11. 范例三:通过json.dumps()编码成json再传入
  12. jsondata = None
  13. postdata = {'fieldName': 't_1', 'fieldKey': 'c_t1', 'dataType': 'TEXT',
  14. 'fieldDesc': 's', 'optiontalFalg': 0, 'optiontalItems': '', 'events': []}
  15. resp = sess.post(url, verify=False, json=jsondata, data=json.dumps(postdata))

保持会话session

有时候我们希望在跨请求中保持同一个cookie,可用到Session();

  1. s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
  2. r = s.get("http://httpbin.org/cookies")
  3. print(r.text)
  4. # '{"cookies": {"sessioncookie": "123456789"}}'

会话结合上下文管理器使用:

  1. with requests.Session() as s:
  2. s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')

response解析

重定向相关

可通过Response.history来追踪重定向;
allow_redirects参数可禁用重定向处理

  1. r = requests.get('http://github.com')
  2. r.history #[<Response [301]>]
  3. # 禁用重定向
  4. r = requests.get('http://github.com', allow_redirects=False)
  5. r.status_code #301
  6. r.history #[]

证书验证相关

verify=True
#Requests可忽略ssl证书的验证

其他

requests.packages.urllib3.disable_warnings()
#忽略警告:InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised.