py requests.post2
Posted rhxuza1993
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了py requests.post2相关的知识,希望对你有一定的参考价值。
# -*- coding: utf-8 -*-
import json
import requests
headers = {‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (Khtml, like Gecko) Chrome/23.0.1271.64 Safari/537.11‘,
‘Accept‘: ‘text/html;q=0.9,*/*;q=0.8‘,
‘Accept-Charset‘: ‘ISO-8859-1,utf-8;q=0.7,*;q=0.3‘,
‘Accept-Encoding‘: ‘gzip‘,
‘Connection‘: ‘close‘,
‘Referer‘: None
}
# 自定义类
class MyClass:
# 初始化
def __init__(self):
print("__init__=====>self: " , "self")
# 创建MyClass对象
myClass = MyClass()
# 对象转化为字典
myClassDict = myClass.__dict__
# 字典转化为json
myClassJson = json.dumps(myClassDict,sort_keys=True, indent=2,ensure_ascii=False)
myClassLoads = json.loads(myClassJson)
def reqSend(obj, url,log):
try:
myClassDict = obj.__dict__
myClassJson = json.dumps(myClassDict, sort_keys=True, indent=2, ensure_ascii=False)
myClassLoads = json.loads(myClassJson)
r1 = requests.post(url, data=myClassLoads, headers=headers)
r1.encoding = ‘utf-8‘
log.warn("***执行接口>>>>>"+url+‘ 【请求参数】 ‘+myClassJson)
r1Text = json.dumps(json.loads(r1.text),sort_keys=True, indent=2,ensure_ascii=False)
log.debug(‘【响应】 ‘+r1Text)
return r1Text;
except:
print("reqSend=====>except: " , "except")
pass
以上是关于py requests.post2的主要内容,如果未能解决你的问题,请参考以下文章