設為首頁收藏本站

艾歐踢論壇

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

搜索
熱搜: 活動 交友 discuz
查看: 291|回復: 0
打印 上一主題 下一主題

用python获取新浪历史分时数据

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2023-4-18 08:46:34 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
用python获取新浪历史分时数据新浪的历史分时数据,经过了压缩和base64处理,解码代码位于sf_sdk.js中,这一行:

this.xh5_S_KLC_D = function(t) {

这个函数用于解码一天的分时数据,最简单的办法,就是把这个函数抠出来,保存到本地,然后通过requests拿到解码前数据,调用这个函数解码即可。

细节就不说了,需要安装PyExecJs库(pip install PyExecJs)

直接上测试代码(代码中引用的sf_sdk.js文件,见后面):

  1. import execjs


  2. def get_last_5days_data(code: str):
  3.     '''获取指定股票最近5天的分时线数据(含当天)'''
  4.     url = 'https://finance.sina.com.cn/realstock/company/%s/hisdata/klc_cm.js' % (
  5.         code)  # 指定日期对应的最近5天的分时数据 KLC_ML_szxxxxxx=
  6.     prefix = 'KLC_ML_%s=' % code
  7.     res = requests.get(url)
  8.     if res.status_code == 200:
  9.         reg = re.compile(r'%s"(.+?)"' % prefix)
  10.         data = reg.findall(res.text)

  11.     with open('D:\data\sf_sdk.js', 'r') as f:
  12.         js_file = f.read()
  13.     ctx = execjs.compile(js_file)
  14.     days_data = data[0].split(',')
  15.     for i in range(len(days_data)):
  16.         arr = ctx.call('decode', days_data[i])
  17.         print(arr[0])


  18. def get_month_data(code: str, date: str):
  19.     '''获取日期所在月份的所有分时数据(如果是当前月,不含当天的)'''
  20.     year = date[0:4]
  21.     month = date[5:7]
  22.     url = 'http://finance.sina.com.cn/realstock/company/%s/hisdata/%s/%s.js?d=%s' % (
  23.     code, year, month, date)  # 指定日期所在月份的所有分时数据  MLC_szxxxxxx_2021_04=
  24.     prefix = 'MLC_%s_%s_%s=' % (code, year, month)
  25.     res = requests.get(url)
  26.     if res.status_code == 200:
  27.         reg = re.compile(r'%s"(.+?)"' % prefix)
  28.         data = reg.findall(res.text)

  29.     with open('D:\data\sf_sdk.js', 'r') as f:
  30.         js_file = f.read()
  31.     ctx = execjs.compile(js_file)
  32.     days_data = data[0].split(',')
  33.     for i in range(len(days_data)):
  34.         arr = ctx.call('decode', days_data[i])
  35.         print(arr[0])



  36. if __name__ == '__main__':
  37.     get_month_data('sz000001', '2022-11-07')
  38.     get_last_5days_data('sz000001')
複製代碼
{'volume': 878600, 'price': 10.38, 'avg_price': 10.38, 'date': '2022-11-01T00:00:00Z', 'prevclose': 10.34}
{'volume': 918223, 'price': 10.61, 'avg_price': 10.61, 'date': '2022-11-02T00:00:00Z', 'prevclose': 10.67}
{'volume': 692100, 'price': 10.54, 'avg_price': 10.54, 'date': '2022-11-03T00:00:00Z', 'prevclose': 10.63}
{'volume': 630600, 'price': 10.4, 'avg_price': 10.4, 'date': '2022-11-04T00:00:00Z', 'prevclose': 10.44}
{'volume': 1915000, 'price': 10.81, 'avg_price': 10.81, 'date': '2022-11-07T00:00:00Z', 'prevclose': 10.82}
{'volume': 455000, 'price': 10.89, 'avg_price': 10.89, 'date': '2022-11-08T00:00:00Z', 'prevclose': 10.88}
{'volume': 297200, 'price': 10.84, 'avg_price': 10.84, 'date': '2022-11-09T00:00:00Z', 'prevclose': 10.85}
{'volume': 860100, 'price': 10.79, 'avg_price': 10.79, 'date': '2022-11-10T00:00:00Z', 'prevclose': 10.89}
{'volume': 2260200, 'price': 11.13, 'avg_price': 11.13, 'date': '2022-11-11T00:00:00Z', 'prevclose': 10.87}
{'volume': 7054841, 'price': 11.7, 'avg_price': 11.7, 'date': '2022-11-14T00:00:00Z', 'prevclose': 11.43}
{'volume': 1660300, 'price': 11.86, 'avg_price': 11.86, 'date': '2022-11-15T00:00:00Z', 'prevclose': 11.95}
{'volume': 766500, 'price': 11.97, 'avg_price': 11.97, 'date': '2022-11-16T00:00:00Z', 'prevclose': 12.01}
{'volume': 448300, 'price': 11.81, 'avg_price': 11.81, 'date': '2022-11-17T00:00:00Z', 'prevclose': 11.82}
{'volume': 664900, 'price': 11.74, 'avg_price': 11.74, 'date': '2022-11-18T00:00:00Z', 'prevclose': 11.69}
{'volume': 964800, 'price': 11.48, 'avg_price': 11.48, 'date': '2022-11-21T00:00:00Z', 'prevclose': 11.59}
{'volume': 658300, 'price': 11.46, 'avg_price': 11.46, 'date': '2022-11-22T00:00:00Z', 'prevclose': 11.46}
{'volume': 429900, 'price': 11.78, 'avg_price': 11.78, 'date': '2022-11-23T00:00:00Z', 'prevclose': 11.82}
{'volume': 929400, 'price': 11.92, 'avg_price': 11.92, 'date': '2022-11-24T00:00:00Z', 'prevclose': 11.82}
{'volume': 858500, 'price': 11.79, 'avg_price': 11.79, 'date': '2022-11-25T00:00:00Z', 'prevclose': 11.77}
{'volume': 2789642, 'price': 11.9, 'avg_price': 11.9, 'date': '2022-11-28T00:00:00Z', 'prevclose': 12.17}
{'volume': 3286300, 'price': 12.16, 'avg_price': 12.16, 'date': '2022-11-29T00:00:00Z', 'prevclose': 11.81}
{'volume': 3005500, 'price': 12.9, 'avg_price': 12.9, 'date': '2022-11-30T00:00:00Z', 'prevclose': 12.99}
{'volume': 2148900, 'price': 12.69, 'avg_price': 12.697, 'date': '2023-04-11T00:00:00Z', 'prevclose': 12.68}
{'volume': 1246700, 'price': 12.57, 'avg_price': 12.572, 'date': '2023-04-12T00:00:00Z', 'prevclose': 12.54}
{'volume': 3107600, 'price': 12.38, 'avg_price': 12.403, 'date': '2023-04-13T00:00:00Z', 'prevclose': 12.48}
{'volume': 2764531, 'price': 12.62, 'avg_price': 12.609, 'date': '2023-04-14T00:00:00Z', 'prevclose': 12.56}
{'volume': 3406596, 'price': 12.62, 'avg_price': 12.643, 'date': '2023-04-17T00:00:00Z', 'prevclose': 12.69}



本帖子中包含更多資源

您需要 登錄 才可以下載或查看,沒有帳號?立即註冊

x
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 轉播轉播 分享分享 分享淘帖
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|Archiver|手機版|艾歐踢創新工坊    

GMT+8, 2024-4-29 18:19 , Processed in 0.247317 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表