这是一篇关于股票数据,落地的实操案例;其中包含了单支股票的分时以及日K数据;欢迎个人学习,禁止商用
案例效果截图展示
使用技术以及工具
脚本语言:python 可视化工具:FineBI 开发工具:PyCharm 实现方式:数据落库,智能报表展示;刷新间隙为5分钟更新 目标网站:东方财富网
实现过程
1.网站分析
东方财务访问网址:
http://quote.eastmoney.com/sz000969.html?returnCode=2
网页解析:
分析:
打开上述页面替换相关股票代码后,按键盘[F12]键或在页面右击鼠标点击检查 进入开发者工具,点击网络(Network)搜索框输入api/qt/stock/kline/get?cb
获取日K数据,输入1
获取日分时URL
api/qt/stock/kline/get?cb 日K-URL-安泰科技
2.实现代码
日K数据抓取:
import re
import uuid
import datetime
import requests
import json
# 替换上述拿到的URL
url = "http://push2.eastmoney.com/api/qt/stock/trends2/get?cb=jQuery1124004210927551632904_1662648655902&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6%2Cf7%2Cf8%2Cf9%2Cf10%2Cf11%2Cf12%2Cf13&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58&ut=fa5fd1943c7b386f172d6893dbfba10b&ndays=1&iscr=0&iscca=0&secid=0.000959&_=1662648655937"
res = requests.get(url)
result = res.text.split("jQuery1124004210927551632904_1662648655902")[1].split("(")[1].split(");")[0]
result_json = json.loads(result)
def save_data(data):
for i in data:
datas = re.split(r'[;,s]s*', i)
uid = str(uuid.uuid4())
for i in range(len(datas)):
if i == 0:
date1 = datas[i]
if i == 1:
s_min = datas[i]
if i == 2:
dx_min = datas[i]
if i == 3:
min_1 = datas[i]
if i == 4:
min_2 = datas[i]
if i == 5:
xc_qty_dx_min = datas[i]
if i == 6:
xc_amt_dx_min = datas[i]
if i == 7:
jx = datas[i]
row = '{},{},{},{},{},{},{},{},{},{}'.format("'"+date1+":01'", str(s_min), str(dx_min), str(min_1), str(min_2),
str(xc_qty_dx_min), str(xc_amt_dx_min), str(jx),"'"+str(''.join(uid.split('-')))+"'","'"+datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")+"'")
row = " INSERT INTO SGPO.PO_SA_STOCK_MINUTE VALUES (" + row + ");"
print(row)
stock_data = result_json['data']['trends']
save_data(stock_data)
分时数据抓取:
import re
import uuid
import datetime
import requests
import json
url = "http://push2.eastmoney.com/api/qt/stock/trends2/get?cb=jQuery1124004210927551632904_1662648655902&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6%2Cf7%2Cf8%2Cf9%2Cf10%2Cf11%2Cf12%2Cf13&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58&ut=fa5fd1943c7b386f172d6893dbfba10b&ndays=1&iscr=0&iscca=0&secid=0.000959&_=1662648655937"
res = requests.get(url)
result = res.text.split("jQuery1124004210927551632904_1662648655902")[1].split("(")[1].split(");")[0]
result_json = json.loads(result)
def save_data(data):
for i in data:
datas = re.split(r'[;,s]s*', i)
uid = str(uuid.uuid4())
for i in range(len(datas)):
if i == 0:
date1 = datas[i]
if i == 1:
s_min = datas[i]
if i == 2:
dx_min = datas[i]
if i == 3:
min_1 = datas[i]
if i == 4:
min_2 = datas[i]
if i == 5:
xc_qty_dx_min = datas[i]
if i == 6:
xc_amt_dx_min = datas[i]
if i == 7:
jx = datas[i]
row = '{},{},{},{},{},{},{},{},{},{}'.format("'"+date1+":01'", str(s_min), str(dx_min), str(min_1), str(min_2),
str(xc_qty_dx_min), str(xc_amt_dx_min), str(jx),"'"+str(''.join(uid.split('-')))+"'","'"+datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")+"'")
row = " INSERT INTO SGPO.PO_SA_STOCK_MINUTE VALUES (" + row + ");"
print(row)
stock_data = result_json['data']['trends']
save_data(stock_data)
3.表结构
日K图基础数据
Name | Postion | Data type | Key Seq | Remarks | Part Key Seq |
---|---|---|---|---|---|
TRADE_DATE | DATENo | 交易日 | |||
open | 1 | DECIMAL(22, 2)No | 今开 | ||
PRE_CLOSE | 2 | DECIMAL(22, 2)No | 昨收 | ||
HIGH | 3 | DECIMAL(22, 2)No | 最高 | ||
LOW | 4 | DECIMAL(22, 2)No | 最低 | ||
VOL | 5 | DECIMAL(22, 2)No | 成交量 | ||
AMOUNT | 6 | DECIMAL(22, 2)No | 成交额 | ||
PRE_PRICE | 7 | DECIMAL(22, 2)No | gj | ||
PCT_CHG | 8 | DECIMAL(22, 2)No | 涨跌幅 | ||
CHANGE | 9 | DECIMAL(22, 2)No | 涨跌点位 | ||
CHANGE_HANDS | 10 | DECIMAL(22, 2)No | 换手率 | ||
UUID | 11 | VARCHAR(64)No | 1 | 唯一码 | 1 |
LAST_REVISE_TIME | 12 | TIMESTAMP | 最近修改时间 |
CREATE TABLE SGPO.PO_SA_STOCK_DAY (
TRADE_DATE DATE,
"open" DECIMAL(22, 2),
PRE_CLOSE DECIMAL(22, 2),
HIGH DECIMAL(22, 2),
LOW DECIMAL(22, 2),
VOL DECIMAL(22, 2),
AMOUNT DECIMAL(22, 2),
PRE_PRICE DECIMAL(22, 2),
PCT_CHG DECIMAL(22, 2),
CHANGE DECIMAL(22, 2),
CHANGE_HANDS DECIMAL(22, 2),
UUID VARCHAR(64) NOT NULL DEFAULT ,
LAST_REVISE_TIME TIMESTAMP
) ;
COMMENT ON TABLE SGPO.PO_SA_STOCK_DAY IS '日K图基础数据';
COMMENT ON SGPO.PO_SA_STOCK_DAY (
TRADE_DATE IS '交易日',
"open" IS '今开',
PRE_CLOSE IS '昨收',
HIGH IS '最高',
LOW IS '最低',
VOL IS '成交量',
AMOUNT IS '成交额',
PRE_PRICE IS 'gj',
PCT_CHG IS '涨跌幅',
CHANGE IS '涨跌点位',
CHANGE_HANDS IS '换手率',
LAST_REVISE_TIME IS '最近修改时间' );
ALTER TABLE SGPO.PO_SA_STOCK_DAY
ADD CONSTRAINT PO_SA_STOCK_DAY_PK PRIMARY KEY
(UUID)
ENFORCED;
COMMIT;
日内分时基础数据
Name | Postion | Data type | Key Seq | Remarks | Part Key Seq |
---|---|---|---|---|---|
TRADE_DATE_MINUTE | DATENo | 交易日 | |||
last_minute_price | 1 | DECIMAL(22, 2)No | 今开 | ||
MINUTE_PRICE | 2 | DECIMAL(22, 2)No | 昨收 | ||
HIGH | 3 | DECIMAL(22, 2)No | 最高 | ||
LOW | 4 | DECIMAL(22, 2)No | 最低 | ||
VOL | 5 | DECIMAL(22, 2)No | 成交量 | ||
AMOUNT | 6 | DECIMAL(22, 2)No | 成交额 | ||
AVG | 7 | DECIMAL(22, 2)No | 均线 | ||
UUID | 11 | VARCHAR(64)No | 1 | 唯一码 | 1 |
LAST_REVISE_TIME | 12 | TIMESTAMP | 最近修改时间 |
CREATE TABLE SGPO.PO_SA_STOCK_MINUTE (
TRADE_DATE_MINUTE TIMESTAMP,
"last_minute_price" DECIMAL(22, 2),
MINUTE_PRICE DECIMAL(22, 2),
HIGH DECIMAL(22, 2),
LOW DECIMAL(22, 2),
VOL DECIMAL(22, 2),
AMOUNT DECIMAL(22, 2),
AVG DECIMAL(22, 2),
UUID VARCHAR(64) NOT NULL DEFAULT ,
LAST_REVISE_TIME TIMESTAMP
) ;
COMMENT ON TABLE SGPO.PO_SA_STOCK_MINUTE IS '日内分时基础数据';
COMMENT ON SGPO.PO_SA_STOCK_MINUTE (
TRADE_DATE_MINUTE IS '交易日-分钟',
"last_minute_price" IS '上分钟股价',
MINUTE_PRICE IS '当分股价',
HIGH IS '股价-未知1',
LOW IS '最低-未知2',
VOL IS '成交量',
AMOUNT IS '成交额',
AVG IS '均线',
LAST_REVISE_TIME IS '最近修改时间' );
ALTER TABLE SGPO.PO_SA_STOCK_MINUTE
ADD CONSTRAINT PO_SA_STOCK_MINUTE_PK PRIMARY KEY
(UUID)
ENFORCED;
COMMIT;