1 Star 0 Fork 0

jx0913/stock

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
collect_data.py 2.40 KB
一键复制 编辑 原始数据 按行查看 历史
Rocky Chen 提交于 2019-06-12 09:59 . 修改最高最低
#-*-coding=utf-8-*-
# 每天收盘收运行
import datetime
import time
__author__ = 'Rocky'
'''
http://30daydo.com
Contact: weigesysu@qq.com
'''
import tushare as ts
import os
from setting import get_engine,llogger,is_holiday,DATA_PATH
import pandas as pd
filename=os.path.basename(__file__)
logger = llogger('log/'+filename)
class SaveData():
today = datetime.datetime.now().strftime("%Y-%m-%d")
daily_engine = get_engine('db_daily')
def __init__(self):
work_space=DATA_PATH
if os.path.exists(work_space) ==False:
os.mkdir(work_space)
os.chdir(work_space)
@staticmethod
def daily_market():
df = ts.get_today_all()
try:
df.to_sql(SaveData.today,SaveData.daily_engine,if_exists='replace')
except Exception as e:
logger.info(e)
logger.info("Save {} data to MySQL".format(SaveData.today))
#获取解禁股
def get_classified_stock(self,year=None,month=None):
df=ts.xsg_data(year,month)
filename='{}-{}-classified_stock.xls'.format(year,month)
self.save_to_excel(df,filename)
def basic_info(self,retry=5):
engine = get_engine('db_stock')
# 需要添加异常处理 重试次数
count = 0
while count < retry:
try:
df = ts.get_stock_basics()
except Exception as e:
logger.info(e)
time.sleep(10)
count+=1
continue
else:
if df is not None:
df=df.reset_index()
df['更新日期']=datetime.datetime.now()
df.to_sql('tb_basic_info',engine,if_exists='replace')
logger.info('入库成功')
break
else:
count+=1
time.sleep(10)
continue
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:
logger.info("Save to excel faile")
logger.info(e)
return None
def main():
obj=SaveData()
obj.basic_info()
if __name__=='__main__':
main()
logger.info('完成')
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jx0913/stock.git
git@gitee.com:jx0913/stock.git
jx0913
stock
stock
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385