requests使用心得

自己在使用requests期间,有些心得和总结,整理如下:

crifan的requests总结

把基于requests的常用功能,封装成函数,供需要的参考:

https://github.com/crifan/crifanLibPython/blob/master/python3/crifanLib/thirdParty/crifanRequests.py

其他心得

get加上参数

举例:

payload = {'key1': 'value1', 'key2': 'value2’}
r = requests.get('https://httpbin.org/get', params=payload)
print(r.url)
# https://httpbin.org/get?key2=value2&key1=value1

header和post的body

举例:

gHeaders = {
  "Authorization": "JWT xxx", # JWT eyJ0xxxxxxxiJ9.xxxxxxxxx.hklGrByjU-v_xxxxx_-dc
}

  postBody = {
    "username": Username,
    "password": Password,
  }
  getTokenResp = requests.post(GetJwtTokenUrl, data=postBody)

saveScriptResp = requests.post(CreateScriptUrl, headers=gHeaders, data=curScriptDict)

传入post的data时,不要dict的json,而是要str的json字符串

注:对标常见情况是,自己把dict的用dict转换成json字符串,再给data

import requests
import json

scritpJsonStr = json.dumps(curScriptDict)
saveScriptResp = requests.post(CreateScriptUrl, headers=gHeaders, data=scritpJsonStr)

或:

或者把dict传递给json,requests自动帮你转换成json

import requests

saveScriptResp = requests.post(CreateScriptUrl, headers=gHeaders, json=curScriptDict)

-> 说明不是两者同时传递的参数,而是二选一

传递json类型参数时,记得加上content-type的header

gHeaders = {
  'Content-Type': 'application/json; charset=utf-8',
  "Accept": 'application/json',
}

否则可能会导致 TypeError string indices must be integers

get返回二进制,保存到文件

下载文件 下载二进制文件

import requests
resp = requests.get(pictureUrl)
with open(saveFullPath, 'wb') as saveFp:
  saveFp.write(resp.content)

举例:

gHeaders = {
  "User-Agent": UserAgent_Mac_Chrome,
}
curPictureUrl = "https://bp.pep.com.cn/ebook/yybanjxc/files/mobile/1.jpg?200209175611"
saveFullPath = "output/义教教科书英语八年级下册/mobile_1.jpg"
resp = requests.get(curPictureUrl, headers=gHeaders)
if resp.ok:
  with open(saveFullPath, 'wb') as saveFp:
    saveFp.write(resp.content)

上传multipart/form-data表单参数和文件

上传multipart/form-data表单form参数

curl命令:

curl -X PUT http://127.0.0.1:8080/api/xxx ...
-H 'content-type: multipart/form-data; boundary=----xxx' \
-F taskStatus=1

对应Pythonrequests的:More complicated POST requests

代码:

    updateTaskUrl = "http://127.0.0.1:8080/api/xxx"
    updateInfoDict = {
        "taskStatus": 1,
    }
    resp = requests.put(updateTaskUrl, data=updateInfoDict)

上传multipart/form-data文件

curl命令:

curl -X POST http://127.0.0.1:8080/api/xxx ...
-H 'content-type: multipart/form-data; boundary=----xxx' \
-F file=@/Users/xxx.txt

对应Pythonrequests的:POST a Multipart-Encoded File

代码:

    filePath = "/Users/xxx.txt"
    fileFp = open(filePath, 'rb')
    fileInfoDict = {
        "file": fileFp,
    }
    resp = requests.post(uploadResultUrl, files=fileInfoDict)

results matching ""

    No results matching ""