从今年一月份开始新浪财经的股票数据接口http://hq.sinajs.cn/list=sz002603,sz002604禁用Http方式调用了,调用会返回“Kinsoku jikou desu!”,导致很多依赖它的股票插件不能使用。为了解决这个问题,开发了“股票盯盘助手”浏览器插件,可以继续使用。
现在有两种方式可以使用新浪财经的股票实时数据。
方式一:增加Referer使用Https调用
现在需要使用https://hq.sinajs.cn/list=sz002603,sz002604,并指定 Referer=https://finance.sina.com.cn 才可以获取数据。
PostMan截图如下:
返回值含义:
0:”大秦铁路”,股票名字; 1:”27.55″,今日开盘价; 2:”27.25″,昨日收盘价; 3:”26.91″,当前价格; 4:”27.55″,今日最高价; 5:”26.20″,今日最低价; 6:”26.91″,竞买价,即“买一”报价; 7:”26.92″,竞卖价,即“卖一”报价; 8:”22114263″,成交的股票数,由于股票交易以一百股为基本单位,所以在使用时,通常把该值除以一百; 9:”589824680″,成交金额,单位为“元”,为了一目了然,通常以“万元”为成交金额的单位,所以通常把该值除以一万; 10:”4695″,“买一”申请4695股,即47手; 11:”26.91″,“买一”报价; 12:”57590″,“买二” 13:”26.90″,“买二” 14:”14700″,“买三” 15:”26.89″,“买三” 16:”14300″,“买四” 17:”26.88″,“买四” 18:”15100″,“买五” 19:”26.87″,“买五” 20:”3100″,“卖一”申报3100股,即31手; 21:”26.92″,“卖一”报价 (22, 23), (24, 25), (26,27), (28, 29)分别为“卖二”至“卖四的情况” 30:”2008-01-11″,日期; 31:”15:05:32″,时间;
因为没有找到在浏览器插件中指定Referer访问https接口的方法,所以这里用C#封装了一下。代码如下:
public class SinaController : Controller { [System.Web.Mvc.HttpGet] public ContentResult list(string id) { try { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | (SecurityProtocolType)3072; var client = new RestClient("https://hq.sinajs.cn/list="+ id); client.Timeout = -1; var request = new RestRequest(Method.GET); request.AddHeader("User-Agent", "PostmanRuntime/7.26.8"); request.AddHeader("Accept", "*/*"); request.AddHeader("Accept-Encoding", "gzip, deflate, br"); request.AddHeader("Content-Type", "application/javascript; charset=GB18030"); request.AddHeader("Content-Encoding", "gzip"); request.AddHeader("Referer", "https://finance.sina.com.cn"); IRestResponse response = client.Execute(request); Encoding gb18030 = Encoding.GetEncoding("GB18030"); Encoding utf8 = Encoding.GetEncoding("UTF-8"); //关键也就是这句了 byte[] utf8Encoding = Encoding.Convert(gb18030, utf8, response.RawBytes); var utf8Text= utf8.GetString(utf8Encoding); return new ContentResult() { Content = utf8Text, ContentEncoding = utf8 }; } catch (Exception) { throw; } } }
方式二:使用新浪云SAE的新浪财经数据接口
首先,注册新浪云SAE的账号
注册地址:https://www.sinacloud.com/public/login/inviter/gaimrn-mddmzeKWrhKWnaoGIgWd-iJGwgIzIdg.html 。使用我这个推广链接注册可以多获得900云豆,够一个月用的了。
然后,创建Python应用
选择Python-共享环境-Git方式就行,最实惠的方案,按量付费+10云豆/天的应用租金。
创建应用后,就可以获取到AccessKey和SecretKey信息了
修改下面代码里SaeApibusAuthHandler方法的参数,上传到Git仓库,使用新浪云给分配的二级域名,就可以访问股票接口数据了。
from bottle import Bottle, route, request, response, run import sae import urllib2 from apibus_handler import SaeApibusAuthHandler app = Bottle() @app.hook('after_request') def enable_cors(): response.headers['Access-Control-Allow-Origin'] = '*' response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS' response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token' @app.route('/') def hello(): try: return "Hello, world! - Bottle" except Exception,err: print 1,err else: print 2 @app.route('/list') def list(): try: stockcode = request.params.get("code") if stockcode.endswith(","): stockcode = stockcode[:-1] apibus_handler = SaeApibusAuthHandler('13mxnn1245', 'zilhjjh2l342yw3jz2hkwm43hhjhl0ymj0x325km') opener = urllib2.build_opener(apibus_handler) stockinfo = opener.open('http://g.sae.sina.com.cn/financehq/list='+stockcode).read() stockinfo = unicode(stockinfo,'GB18030').encode('UTF-8') stockinfoArray = stockinfo.splitlines() stockcodeArray = stockcode.split(',') ret = '' if len(stockinfoArray)==len(stockcodeArray): for index in range(len(stockinfoArray)): ret += 'hq_str_'+stockcodeArray[index]+'="'+stockinfoArray[index]+'";\r\n' else: ret = stockinfo return ret except Exception,err: print 1,err return err else: print 2 application = sae.create_wsgi_app(app)
写在最后
后期如何搭建新浪SAE的本地开发环境,坑比较多,有兴趣的可以找我私聊。
基于新浪股票接口我做了个浏览器插件“股票盯盘助手”,欢迎使用!
SAE注册地址:https://www.sinacloud.com/public/login/inviter/gaimrn-mddmzeKWrhKWnaoGIgWd-iJGwgIzIdg.html