從今年一月份開始新浪財經的股票數據接口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