python3發(fā)送request請求及查看返回結(jié)果實例
我就廢話不多說了,大家還是直接看代碼吧!
import requests
import json
raw = {}
headers中添加上content-type這個參數(shù),指定為json格式
headers = {‘Content-Type': ‘a(chǎn)pplication/json'}
post的時候,將data字典形式的參數(shù)用json包轉(zhuǎn)換成json格式。
response = requests.post(url=‘XXXX',
headers=headers, data=json.dumps(raw))
返回信息
print(response.text)
返回響應頭
print(response.status_code)
補充知識:python3使用requests發(fā)送post請求,對接口返回的數(shù)據(jù)進行處理,最后塞入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 == "修改狀態(tài)為 已解決":
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發(fā)送request請求及查看返回結(jié)果實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python+pytest接口自動化參數(shù)關(guān)聯(lián)
這篇文章主要介紹了python+pytest接口自動化參數(shù)關(guān)聯(lián),參數(shù)關(guān)聯(lián),也叫接口關(guān)聯(lián),即接口之間存在參數(shù)的聯(lián)系或依賴,更多相關(guān)內(nèi)容需要的小伙伴可可以參考一下2022-06-06
Python騷操作完美實現(xiàn)短視頻偽原創(chuàng)
剪輯的視頻上傳到某平臺碰到降權(quán)怎么辦?視頻平臺都有一套自己的鑒別算法,專門用于處理視頻的二次剪輯,本篇我們來用python做一些特殊處理2022-02-02
Python2 Selenium元素定位的實現(xiàn)(8種)
這篇文章主要介紹了Python2 Selenium元素定位的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02
Python實現(xiàn)將實體類列表數(shù)據(jù)導出到Excel文件
在數(shù)據(jù)處理和報告生成中,將實體類的列表數(shù)據(jù)導出到Excel文件是一項常見任務(wù),Python提供了多種庫來實現(xiàn)這一目標,下面就來跟隨小編一起學習一下吧2025-01-01
Python+Selenium自動化環(huán)境搭建與操作基礎(chǔ)詳解
Selenium是如今最常用的自動化測試工具之一,支持快速開發(fā)自動化測試框架,且支持在多種瀏覽器上執(zhí)行測試。本文將介紹關(guān)于Selenium?Python自動化腳本環(huán)境搭建的相關(guān)資料,需要的朋友可以參考下2022-03-03

