发现和讯网站也提供了股票交易的接口,只是和其他网站一样都没有正式公开的文档,只有自己摸索了.好在我需要的数据也不多,不需要详细分析返回的数据格式,能用就行.
对我来说,和讯比新浪的接口更好的一点是和讯的净值数据可以指定日期,同时是XML格式,方便解析.
如果不怕麻烦的话,其实就可以发现网易/新浪/腾讯/和讯/指南针等不少网站都有股票交易的数据接口.随便列举一些样例:
http://hq.sinajs.cn/list=sz150018,f_150018,sz150019,f_150019,f_161812
http://data.funds.hexun.com/outxml/detail/openfundnetvalue.aspx?fundcode=150019&startdate=2013-01-10&enddate=2013-01-18
http://quote.stock.hexun.com/stockdata/fund_quote.aspx?stocklist=150019
http://quotes.fund.163.com/ajax/detail/netvalue/150018/20121201-20130117.html
当然,我的要求不高,所以没有必要详细分析这些接口了.写点代码完成我的要求吧.当然高手和忍受不了代码"臭"味道的朋友最好飘过. 以下代码无论从命名到定义都有不少不和规范的, 就不一一解释了..
#-*- coding:utf-8 -*- import urllib2 import datetime import re def download_html(url): f = urllib2.urlopen(url) return f.read() def download_price(fcode): url = 'http://quote.stock.hexun.com/stockdata/fund_quote.aspx?stocklist=%s' % fcode return download_html(url) def download_unitvalue(fcode): startdate, enddate = calc_date() url = 'http://data.funds.hexun.com/outxml/detail/openfundnetvalue.aspx?fundcode=%s&startdate=%s&enddate=%s' % (fcode, startdate, enddate) #print url return download_html(url) def calc_date(): today = datetime.date.today() day = datetime.timedelta(days=1) startdate = today - day - day enddate = today + day return startdate.strftime("%Y-%m-%d"), enddate.strftime("%Y-%m-%d") def now_date(): today = datetime.date.today() return today.strftime("%Y-%m-%d") def parse_price(html): items = html.split(',') return items[2] def get_price(fcode): html = download_price(fcode) return parse_price(html) def parse_item(data, head, tail): pattern = '%s\s*(.*?)\s*%s' % (head, tail) #print pattern prog = re.compile(pattern, re.I | re.M | re.S) mo = prog.search(data) return mo.group(1) def parse_unitvalue(html): date = parse_item(html, '<fld_enddate>', '</fld_enddate>') value = parse_item(html, '<fld_unitnetvalue>', '</fld_unitnetvalue>') return (date, value) def get_unitvalue(fcode): html = download_unitvalue(fcode) return parse_unitvalue(html) def parse_fund(a, b, c): ssa = get_price(a) sfa = get_unitvalue(a) ssb = get_price(b) sfb = get_unitvalue(b) sfc = get_unitvalue(c) sa = float(ssa) sb = float(ssb) fa = float(sfa[1]) fb = float(sfb[1]) fc = float(sfc[1]) assert(sfa[0] == sfb[0]) assert(sfa[0] == sfc[0]) #assert(sfa[0] == now_date()) sc = (sa + sb) / 2 pp = 100.0 * (sc - fc) / fc return (sfa[0], fa, fb, fc, sa, sb, sc, pp) def play_game(): a = '150018' b = '150019' c = '161812' fund = parse_fund(a, b, c) print '%s : %5.3f, %5.3f, %5.3f, %s : %5.3f, %5.3f, %5.3f, %5.3f%%' % (fund[0], fund[1], fund[2], fund[3], now_date(), fund[4], fund[5], fund[6], fund[7]) def test_calc_date(): startdate, enddate = calc_date() today = now_date() print startdate print enddate print today def test_parse_price(): html = "dataArr = [['150019','xxx',0.70,2.63,0.69,0.69,0.72,0.68,994795375,697633497,70.22,4.67,8734900.00,0.70,473730671,506245322,0.86, 'xxx300', 2595.44, 1.67]];NewQuoteListPage.GetData(dataArr);" price = parse_price(html) print price def test_get_price(): fcode = '150019' price = get_price(fcode) print price def test_parse_item(): html = '<2013-01-18>' date = parse_item(html, '<', '>') print date def test_parse_item1(): html = ''' <fld_enddate>2013-01-18</fld_enddate> <fld_unitnetvalue>0.6330</fld_unitnetvalue> <fld_enddate>2013-01-17</fld_enddate> <fld_unitnetvalue>0.6220</fld_unitnetvalue> ''' #html = '<fld_enddate>2013-01-18</fld_enddate>' date = parse_item(html, '<fld_enddate>', '</fld_enddate>') value = parse_item(html, '<fld_unitnetvalue>', '</fld_unitnetvalue>') print date print value def test_download_unitvalue(): fcode = '150019' data = download_unitvalue(fcode) print data def test_get_unitvalue(): fcode = '150019' uv = get_unitvalue(fcode) print uv if __name__ == '__main__': play_game() #test_calc_date() #test_parse_price() #test_get_price() #test_parse_item() #test_parse_item1() #test_get_unitvalue() #test_download_unitvalue()