深交所https://www.szse.cn/aboutus/calendar/index.html网站提供了交易所的交易日历,如下图:
在Edge浏览器中,打开网页后,F12进入开发工具,然后对交易日历切换(比如从2月切换成4月),可以在“网络”项中,发现发生变化的html页面地址。如下图:
可以看到框中的html地址对应的预览数据是json,由此找到了交易日历对应的网址:
http://www.szse.cn/api/report/exchange/onepersistenthour/monthList?month=2024-04
可以由这个网址,构建其他年度和月度的交易日历,以2024年为例:
- import requests
- import pandas as pd
- import time
-
- dt = []
- df = pd.DataFrame()
- year = 2024
-
- hds = {
- "Accept": "application/json, text/javascript, */*; q=0.01",
- "Accept-Encoding": "gzip, deflate",
- "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
- "Connection": "keep-alive",
- "Content-Type": "application/json",
- "Host": "www.szse.cn",
- "Referer": "http://www.szse.cn/aboutus/calendar/index.html",
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0",
- "X-Request-Type": "ajax",
- "X-Requested-With": "XMLHttpRequest"
- }
-
- for i in range(1,13):
- #print(i)
- mth = str(i).rjust(2,'0')
- url = 'http://www.szse.cn/api/report/exchange/onepersistenthour/monthList?month=2024-%s' %(mth)
- t = requests.get(url, headers = hds)
- dt.extend(t.json()['data'])
- time.sleep(3)#控制频率
-
- df = pd.DataFrame(dt)
-
- df.reset_index(inplace=True,drop=True)
- print(df)
打印df,jybz==1表明是交易日,为开市日。