数据结构
每个Json文件包括150个股票,每只股票包括股票代码和可查询到的所有历史信息。
例:[‘2021-12-31’, ‘89.95’, ‘89.95’, ‘0.00’, ‘0.00%’, ‘89.40’, ‘90.54’, ‘4265’, ‘3838.19’, ‘2.13%’]
日期 | 今日开盘价 | 今日收盘价 | 涨跌幅度 | 涨跌百分比 | 今日最低价 | 今日最高价 | 今日成交股票手数 | 今日成交金额(单位:万) | 换手率 |
2021-12-31 | 89.95 | 89.95 | 0.00 | 0.00% | 89.40 | 90.54 | 4265 | 3838.19 | 2.12% |
Json文件对应股票代码
Json文件 | Code |
data1.json | 000001——000564 |
data2.json | 000565——000786 |
data3.json | 000788——000999 |
data4.json | 001201——002125 |
data5.json | 002126——002278 |
data6.json | 002279——002429 |
data7.json | 002430——002582 |
data8.json | 002583——002738 |
data9.json | 002739——002900 |
data10.json | 002901——300018 |
data11.json | 300019——100173 |
data12.json | 300174——300325 |
data13.json | 300326——200479 |
data14.json | 300480——300632 |
data15.json | 300633——300791 |
data16.json | 300792——300950 |
data17.json | 300951——301138 |
python代码
import json
import requests
hd = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/99.0.4844.82 Safari/537.36'}
url = 'https://xuanguapi.eastmoney.com/Stock/JS.aspx?type=xgq&sty=xgq&token=eastmoney&c=[gf2]&p=1&jn=kuAPFImR&ps=3000&s=stockcode&st=1&r=1659237663728'
response = requests.get(url, headers=hd)
info = response.text
info = info[13:len(info)]
info = json.loads(info)
code_all = []
for i in range(len(info['Results'])):
code_all.append(info['Results'][i][2:8])
date_all = []
num = 0
num_0 = 0
for i in range(len(code_all)):
cn_code = code_all[i]
cn_code = cn_code.zfill(6)
start_time = '19900101'
end_time = '20220101'
url = 'https://q.stock.sohu.com/hisHq?code=cn_' + cn_code + '&start=' + start_time + '&end=' + end_time
response = requests.get(url, headers=hd)
info = response.json()
try:
if info[0]['status'] == 0:
date_all.append([cn_code, info[0]['hq']])
num = num + 1
print(cn_code)
except:
continueif num == 150:
num = 0
num_0 = num_0 + 1
with open('data' + str(num_0) + '.json', 'w') as f:
json.dump(date_all, f)
print('ok')
date_all = []
Comments