聚宽没有提供恒生的数据,但是可以通过获取腾讯股票的接口来取。
import json import requests import pandas as pd # 下载恒生指数 html = requests.get('http://web.ifzq.gtimg.cn/appstock/app/hkfqkline/get?param=hkHSI,day,,,250,qfq').content obj = json.loads(html) hk=pd.DataFrame(obj['data']['hkHSI']['day']) hk=hk[[0,1,2,3,4,5]] hk.columns=['date','open', 'close', 'high', 'low', 'amount'] hk['open']=hk.open.astype('float') hk['close']=hk.close.astype('float') hk['high']=hk.high.astype('float') hk['low']=hk.low.astype('float') hk['amount']=hk.amount.astype('float') hk.index = hk['date'] print(hk.loc[hk['date']=='2022-09-07']) print(hk[(hk['date'] >= '2022-09-01') & (hk['date'] <= '2022-09-05')])