怎样开发Python股票接口程序获取股票行情数据?

Python股票接口程序获取国内股票数据程序如下:

  1. def getChinaStockIndividualInfo(stockCode, period):
  2.  try:
  3.  exchange = "sh" if (int(stockCode) // 100000 == 6) else "sz"
  4.  dataUrl = "//hq.sinajs.cn/list=" + exchange + stockCode
  5.  stdout = urllib.request.urlopen(dataUrl)
  6.  stdoutInfo = stdout.read().decode('gb2312')
  7.  tempData = re.search('''(")(.+)(")''', stdoutInfo).group(2)
  8.  stockInfo = tempData.split(",")
  9.  #stockCode = stockCode,
  10.  stockName = stockInfo[0] #名称
  11.  stockStart = stockInfo[1] #开盘
  12.  stockLastEnd= stockInfo[2] #昨收盘
  13.  stockCur = stockInfo[3] #当前
  14.  stockMax = stockInfo[4] #最高
  15.  stockMin = stockInfo[5] #最低
  16.  stockUp = round(float(stockCur) - float(stockLastEnd), 2)
  17.  stockRange = round(float(stockUp) / float(stockLastEnd), 4) * 100
  18.  stockVolume = round(float(stockInfo[8]) / (100 * 10000), 2)
  19.  stockMoney = round(float(stockInfo[9]) / (100000000), 2)
  20.  stockTime = stockInfo[31]
  21.  content = "#" + stockName + "#(" + stockCode + ")" + " 开盘:" + stockStart \
  22.  + ",最新:" + stockCur + ",最高:" + stockMax + ",最低:" + stockMin \
  23.  + ",涨跌:" + str(stockUp) + ",幅度:" + str(stockRange) + "%" \
  24.  + ",总手:" + str(stockVolume) + "万" + ",金额:" + str(stockMoney) \
  25.  + "亿" + ",更新时间:" + stockTime + " "
  26.  imgUrl = "http://image.sinajs.cn/newchart/" + period + "/n/" + exchange + str(stockCode) + ".gif"
  27.  twitter = {'message': content, 'image': imgUrl}
  28.  except Exception as e:
  29.  print(">>>>>> Exception: " + str(e))
  30.  else:
  31.  return twitter
  32.  finally:
  33.  None

  1. Python股票接口挖掘全球股票指数程序如下:
  2. def getWorldStockIndexInfo(stockDict):
  3.  try:
  4.  #http://download.finance.yahoo.com/d/quotes.csv?s=^IXIC&f=sl1c1p2l
  5.  yahooCode = stockDict['yahoo']
  6.  dataUrl = "http://download.finance.yahoo.com/d/quotes.csv?s=" + yahooCode + "&f=sl1c1p2l"
  7.  stdout = urllib.request.urlopen(dataUrl)
  8.  stdoutInfo = stdout.read().decode('gb2312')
  9.  tempData = stdoutInfo.replace('"', '')
  10.  stockInfo = tempData.split(",")
  11.  stockNameCn = stockDict['name']['chinese']
  12.  stockNameEn = stockDict['name']['english']
  13.  stockCode = stockDict['code']
  14.  stockEnd = stockInfo[1] #当前价,5点后为收盘价
  15.  stockZD = stockInfo[2] #涨跌
  16.  stockLastEnd= str(float(stockEnd) - float(stockZD)) #开盘价
  17.  stockFD = stockInfo[3] #幅度
  18.  percent = float(stockFD.replace("%", ""))
  19.  matchResult = re.search("([\w?\s?:]*)(\-)", stockInfo[4]) #日期和最新值
  20.  stockDate = matchResult.group(1)
  21.  content = "#" + stockNameCn + "# " + stockNameEn + "(" + stockCode + ")" \
  22.  + " 当前:" + stockEnd + ", 涨跌:" + stockZD + ", 幅度:" + stockFD \
  23.  + ", 最后交易时间:" + stockDate
  24.  twitter = content
  25.  except Exception as err:
  26.  print(">>>>>> Exception: " + yahooCode + " " + str(err))
  27.  else:
  28.  return twitter
  29.  finally:
  30.  None
  31. def test_china_index_data():
  32.  for stockCode in ChinaStockIndexList:
  33.  twitter = getChinaStockIndexInfo(stockCode, period_daily)
  34.  print(twitter['message'] + twitter['image'])
  35. def test_china_individual_data():
  36.  for stockCode in ChinaStockIndividualList:
  37.  twitter = getChinaStockIndividualInfo(stockCode, period_min)
  38.  print(twitter['message'] + twitter['image'])
  39. def test_global_index_data():
  40.  for stockDict in WorldStockIndexList:
  41.  print(getWorldStockIndexInfo(stockDict))
  42. def main():
  43.  "main function"
  44.  print(.b64decode(b'Q29weXJpZ2h0IChjKSAyMDEyIERvdWN1YmUgSW5jLiBBbGwgcmlnaHRzIHJlc2VydmVkLg==').decode())
  45.  test_china_index_data()
  46.  test_china_individual_data()
  47.  test_global_index_data()
  48. if __name__ == '__main__':
  49.  main()

你可能感兴趣的:(L2接口,python,量化接口,python,servlet,开发语言,java)