1.首先封装post请求:
这里有一个小坑就是你的data数据,需要进行转换,需要安装一个demjson库,进行格式转换,如果直接传入数据没有问题的话就不用转换。这里我做了一个小小的判断,如果是表单形式的数据就不用转换数据直接使用就行。
import requests
import json
import demjson
class RunMain():
def send_post(self, url, data, header,cookie):
if type(data) == type({}): # 传入的请求体为字典类型
if header['Content-Type'] == 'application/json':
data = demjson.encode(data)
elif header['Content-Type'] == 'application/x-www-form-urlencoded':
data = data
else:
pass # 如果是其他的Content-Type类型另做处理
result = requests.post(url=url, data=data, headers=header, cookies=cookie)
re=result
try:
return re.json()
except:
return re.json()
else:
print('请求体错误,请更改请