admin 發表於 2023-4-18 08:46:34

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

用python获取新浪历史分时数据新浪的历史分时数据,经过了压缩和base64处理,解码代码位于sf_sdk.js中,这一行:

this.xh5_S_KLC_D = function(t) {

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

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

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

import execjs


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

    with open('D:\data\sf_sdk.js', 'r') as f:
      js_file = f.read()
    ctx = execjs.compile(js_file)
    days_data = data.split(',')
    for i in range(len(days_data)):
      arr = ctx.call('decode', days_data)
      print(arr)


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

    with open('D:\data\sf_sdk.js', 'r') as f:
      js_file = f.read()
    ctx = execjs.compile(js_file)
    days_data = data.split(',')
    for i in range(len(days_data)):
      arr = ctx.call('decode', days_data)
      print(arr)



if __name__ == '__main__':
    get_month_data('sz000001', '2022-11-07')
    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}


頁: [1]
查看完整版本: 用python获取新浪历史分时数据