总结结果
详细代码:
# -*- coding: utf-8 -*-
import requests
import json
class web_requests(object):
def __init__(self):
pass
def Interface(self,Interface_path,**My_data):
通达信 实时数据接口6,url = 'http://testdj.XXXXXX.com:8378/%s' %(Interface_path) # 测试的接口通达信 实时数据接口6,url
headers = {'Host': 'testdj.XXXXXX.com:8378',
'Accept': 'application/json, text/plain, */*',
'Referer': 'http://testdj.XXXXXX.com:8280/',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Origin': 'http://testdj.XXXXXX.com:8280',
'Accept-Encoding': 'gzip, deflate',
'Accept': 'application/json, text/plain, */*',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36'} #消息头
#data1 = {'beginDate': '2018-01-01', 'endDate': '2018-04-01'} # 接口传送的参数
data = My_data # 接口传送的参数
r = requests.get(通达信 实时数据接口6,url=通达信 实时数据接口6,url, json=data, headers=headers) # 发送请求
# return r.json
print (r.text) # 获取响应报文
print (r.status_code)
a = web_requests()
a.Interface('monitor/sxsb-sxzl',beginDate='2018-01-01', endDate='2018-04-01')
代码说明:
接口的URL:
请求消息头:
headers={'Host':'testdXXXXXX.com:8378','Accept':'application/json,text/plain,*/*','Referer':'http://testdXXXXXX.com:8280/','Accept-Language':'zh-CN,zh;q=0.8','Origin':'http://testdXXXXXX.com:8280','Accept-Encoding':'gzip,deflate','Accept':'application/json,text/plain,*/*','User-Agent':'Mozilla/0(WindowsNT1;Win64;x6AppleWebKit/5336(KHTML,likeGecko)Chrome/60.31679Safari/5336'}#消息头
接口的传参数:
data=My_data#接口传送的参数
注:
python传参数的格式原本如下:
data={'beginDate':'2018-01-01','endDate':'2018-04-01'}#接口传送的参数
但为了方便给外部引用进来,做了如下特殊处理:
这里存在多个参数
Interface('monitor/sxsb-sxzl',beginDate='2018-01-01',endDate='2018-04-01')
python将多个参数合并,重点是“**”
调用时可以直接写:
data=My_data#接口传送的参数
相当于:data={'beginDate':'2018-01-01','endDate':'2018-04-01'}
给服务器发起请求:
r=requests.get(通达信 实时数据接口6,url=通达信 实时数据接口6,url,json=data,headers=headers)#发送请求
服务器返回结果:
print(r.text)#获取响应报文
Robot引用:
py代码
代码如下:
#-*-coding:utf-8-*-
通达信 实时数据接口6,url='http://testdXXXXXX.com:8378%s'%(Interface_pat#测试的接口通达信 实时数据接口6,url
'Accept':'application/json,text/plain,*/*',
'Accept-Language':'zh-CN,zh;q=0.8',
'Accept-Encoding':'gzip,deflate',
'Accept':'application/json,text/plain,*/*',
'User-Agent':'Mozilla/0(WindowsNT1;Win64;x6AppleWebKit/5336(KHTML,likeGecko)Chrome/60.31679Safari/5336'}#消息头
#data1={'beginDate':'2018-01-01','endDate':'2018-04-01'}#接口传送的参数
data=My_data#接口传送的参数
r=requests.get(通达信 实时数据接口6,url=通达信 实时数据接口6,url,json=data,headers=headers)#发送请求
#print(r.text)#获取响应报文
#print(r.status_cod#获取结果码
引用py脚本
robot使用
输出结果
参见问题:
python接口能返回结果,但在获取结果里的data值却报错了
原因是接口返回结果没有转换转成obj
原代码:
用jsoloads修复如下:
文章为作者独立观点,不代表 股票程序化软件自动交易接口观点