我就废话不多说了,大家还是直接看代码吧!
import requests
import json
raw = {}
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-8
import http.cookiejar
import json
from urllib import parse,request
import requests
from openpyxl import workbook
import datetime
name = 'tarcuper'
allowed_domains = ['*']
start_urls1 = 'https://www.tracup.com/api/issues/view'
start_urls2 = 'https://www.tracup.com/api/issues/gettrendlist'
cookies = {
"acw_tc": "78197306155549842936beitidaile9532fb18575fad1fc268ee",
"_ga": "ga1.2.1414148155.1555498434",
"phpsessid": "q7hj5m5neitidaile95efop7eee37ki",
"_gid": "ga1.2.1742874168.1557716531",
"pgyx2_session": "wn99zyrethkeqal92z0tyfp1w7u3wmisn8mhrixap7m4phycerp8p9x7rlr4p3zihkid%2b2spej%2bwq2l%2fvzlv7dqvmqhvzaednmtr88kwpcxmb7u%2fcuasbrfr7ha6gtxfsezwclbcwkhnvanq0pd2uh5u051o1gh3ducujpugaydrckg65bg7ae%2bsvx4pgquyhb00%2f%2fvh3cpgtbtnhdwiolra7qrhh6k5owqlfgih1ugvnd69lyeitidaile95wyxijzc4xtuoja7yyktqren2p92e%2brwxccrjchwm6vs9jw1irgdfo%2bxzg4ylpzfctfgk0rmgx%2fr3n9cc%2fmde1asmg58xcdyvbt9bz4fe53cr1ujrmx9mwwehyzt5vcbrt%2fiqhcapzrepr7qgsdq0zafpfvzga1rptsfxqrfn%2bguhphly%2bnu0fk4n%2f99zwig7rrxk%2bpybm2gqz2jz5kyxvagiq%2bq4%3d"
}
headers = {
"host": "www.tracup.com",
"connection": "keep-alive",
"cache-control": "no-cache",
"pragma": "no-cache",
"if-modified-since": "0",
"user-agent": "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/73.0.3683.103 safari/537.36",
"accept": "*/*",
"referer": "https://www.tracup.com/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_created
wb = 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请求及查看返回结果实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持萬仟网。
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!