forked from Rockyzsu/stock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_market_info.py
More file actions
92 lines (73 loc) · 2.35 KB
/
basic_market_info.py
File metadata and controls
92 lines (73 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
__author__ = 'Rocky'
'''
http://30daydo.com
Contact: weigesysu@qq.com
'''
import datetime
import time
import tushare as ts
import os
from settings import get_engine,llogger,is_holiday,DATA_PATH
import pandas as pd
logger = llogger('log/collect_data.log')
# 获取市场全貌
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('完成')