python3发送request请求及查看返回结果实例

时间:2021-05-22

我就废话不多说了,大家还是直接看代码吧!

import requestsimport jsonraw = {}

headers中添加上content-type这个参数,指定为json格式

headers = {‘Content-Type': ‘application/json'}

post的时候,将data字典形式的参数用json包转换成json格式。

response = requests.post(url=‘XXXX',
headers=headers, data=json.dumps(raw))

返回信息

print(response.text)

返回响应头

print(response.status_code)

补充知识:python3使用requests发送post请求,对接口返回的数据进行处理,最后塞入list,保存到本地的excel中

我就废话不多说了,大家还是直接看代码吧!

#coding:utf-8import http.cookiejarimport jsonfrom urllib import parse,requestimport requestsfrom openpyxl import Workbookimport datetimename = 'tarcuper'allowed_domains = ['*']start_urls1 = 'https:///cloud/", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-CN,zh;q=0.9",}def request2(): req2 = requests.post(url=start_urls2, data=data, headers=headers, cookies=cookies) res2 = req2.text res2 = json.loads(res2) bbb = (res2['data']) # print(bbb) # print('上面bbb,下面cccc') ccc = (bbb['list']) # print(ccc) for var in ccc: iac_created = var.get('iac_created') log = var.get('log') # print(log) # print(iac_created) if log == "修改状态为 已解决": return iac_createdwb = Workbook()ws = wb.create_sheet("che")for i in range(1,500): data = { "pKey":"916338276d07f3c1cacc662b2afa93c2", "iNo":str(i), } req = requests.post(url=start_urls1, data=data, headers=headers, cookies=cookies) res = req.text res = json.loads(res) aaa = (res['data']) _alist = [] j = 0 try: iac_created = request2() i_no = aaa['i_no'] author = aaa['author'] assigner = aaa['assigner'] i_created = aaa['i_created'] i_updated = aaa['i_updated'] i_status = aaa['i_status'] except: pass if i_status == 'a3f47781286ee2ba2bbefbebd0bea5bc' and iac_created != None: _alist.append(i_no) _alist.append(author) _alist.append(assigner) _alist.append(i_created) _alist.append(iac_created) d1 = datetime.datetime.strptime(i_created, '%Y-%m-%d %H:%M') d2 = datetime.datetime.strptime(iac_created, '%Y-%m-%d %H:%M:%S') d = d2-d1 days = d.days delta = d.seconds h = round(days*24+delta/3600) _alist.append(h) print(_alist) ws.append(_alist) wb.save("aaa.xlsx")

以上这篇python3发送request请求及查看返回结果实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章