1 Star 0 Fork 1

ichao1214 / stock

forked from rzxxysy / stock 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
fetch_each_day.py 3.56 KB
一键复制 编辑 原始数据 按行查看 历史
Rockyzsu 提交于 2020-08-05 17:33 . update
# -*-coding=utf-8-*-
__author__ = 'Rocky'
'''
http://30daydo.com
Contact: weigesysu@qq.com
'''
# 获取每天的行情信息
import tushare as ts
import pandas as pd
import time
import datetime
import os
from settings import DBSelector, llogger, is_holiday, DATA_PATH
logger = llogger('log/fetch_each_day.log')
class FetchDaily(object):
def __init__(self):
self.today = datetime.datetime.now().strftime('%Y-%m-%d')
# self.today = '2020-02-07'
self.path = DATA_PATH
if not os.path.exists(self.path):
try:
os.mkdir(self.path)
except Exception as e:
print(e)
self.df_today_all = pd.DataFrame()
self.TIMEOUT = 10
self.DB = DBSelector()
def gettodaymarket(self, re_try=10):
while re_try > 0:
try:
df = ts.get_today_all()
if df is None:
continue
if len(df) == 0:
continue
except Exception as e:
logger.error(e)
re_try = re_try - 1
time.sleep(self.TIMEOUT)
# import tushare as ts
else:
return df
return None
def store(self):
self.df_today_all = self.gettodaymarket()
# 存储每天 涨幅排行 榜,避免每次读取耗时过长
filename = self.today + '_all_.xls'
# 放在data文件夹下
full_filename = os.path.join(self.path, filename)
if self.df_today_all is not None:
# 保留小数点的后两位数
self.df_today_all['turnoverratio'] = self.df_today_all['turnoverratio'].map(lambda x: round(x, 2))
self.df_today_all['per'] = self.df_today_all['per'].map(lambda x: round(x, 2))
self.df_today_all['pb'] = self.df_today_all['pb'].map(lambda x: round(x, 2))
try:
self.df_today_all.to_excel(full_filename)
except Exception as e:
logger.error(e)
engine = self.DB.get_engine('db_daily','qq')
# print(self.df_today_all)
try:
self.df_today_all.to_sql(self.today, engine, if_exists='fail')
except Exception as e:
# print(e)
logger.error(e)
else:
logger.error('today_all df is None')
def store_new(self):
self.df_today_all = self.gettodaymarket()
filename = self.today + '_all_.xls'
full_filename = os.path.join(self.path, filename)
if not os.path.exists(full_filename):
if self.df_today_all is not None:
try:
self.save_to_excel(self.df_today_all, full_filename)
except Exception as e:
print(e)
engine = self.DB.get_engine('db_daily','qq')
try:
self.df_today_all.to_sql(self.today, engine)
except Exception as e:
print(e)
pass
def save_to_excel(self, df, filename, encoding='gbk'):
try:
df.to_csv('temp.csv', encoding=encoding, index=False)
df = pd.read_csv('temp.csv', encoding=encoding, dtype={'code': str})
df.to_excel(filename, encoding=encoding)
return True
except Exception as e:
print("Save to excel faile")
print(e)
return None
if __name__ == "__main__":
if is_holiday():
logger.info("Holidy")
exit()
logger.info("Start")
obj = FetchDaily()
obj.store()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ichao1214/stock.git
git@gitee.com:ichao1214/stock.git
ichao1214
stock
stock
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891