Created
March 12, 2021 03:21
-
-
Save gitchs/b4363e46f311a1d26cb5f1f6e4752fde to your computer and use it in GitHub Desktop.
下载A股交易日历
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
从深圳交易所官网下载交易日历 | |
深圳交易所:http://www.szse.cn/aboutus/calendar/ | |
{ | |
"zrxh": 2, -- 意义不明 | |
"jybz": "1", -- 是否交易日,1是,0否;类型是str | |
"jyrq": "2021-03-01" -- 日历日期 | |
} | |
""" | |
import csv | |
import random | |
import datetime | |
import requests | |
def next_month(date): | |
if date.month == 12: | |
return datetime.datetime(date.year + 1, 1, 1) | |
return datetime.datetime(date.year, date.month+1, 1) | |
def list_dates(date): | |
url = 'http://www.szse.cn/api/report/exchange/onepersistenthour/monthList' | |
params = { | |
'month': date.strftime('%Y-%m'), | |
'random': random.random(), | |
} | |
response = requests.get(url, params=params) | |
payload = response.json() | |
for d in payload['data']: | |
yield d | |
start = datetime.datetime(2010, 1, 1) | |
with open('calendar.csv', 'w') as fd: | |
w = None | |
for d in list_dates(start): | |
if w is None: | |
w = csv.DictWriter(fd, fieldnames=list(d.keys())) | |
w.writeheader() | |
w.writerow(d) | |
start = next_month(start) | |
while start < datetime.datetime(2021, 12, 31): | |
for d in list_dates(start): | |
w.writerow(d) | |
start = next_month(start) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment