forked from Rockyzsu/stock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shareholder_info.py
156 lines (124 loc) · 4.33 KB
/
shareholder_info.py
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# -*- coding: utf-8 -*-
# @Time : 2019/1/19 14:37
# @File : stockholder_info.py
# 股东信息获取
import pandas as pd
import time
import pymysql
import tushare as ts
import config
from settings import get_mysql_conn
conn = get_mysql_conn('db_stock', 'local')
cursor = conn.cursor()
token = config.token
ts.set_token(token)
pro = ts.pro_api()
def get_stock_list():
df = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
return dict(zip(list(df['ts_code'].values), list(df['name'].values)))
# 生产日期 2000到2018
def create_date():
start_date = '20{}0101'
end_date = '20{}1231'
date_list = []
for i in range(18, 0, -1):
print(start_date.format(str(i).zfill(2)))
print(end_date.format(str(i).zfill(2)))
date_list.append(i)
return date_list
# 十大和十大流通
def get_stockholder(code, start, end):
# 十大非流通
global pro
try:
stockholder = pro.top10_holders(ts_code=code, start_date=start, end_date=end)
time.sleep(1)
stockfloat = pro.top10_floatholders(ts_code=code, start_date=start, end_date=end)
time.sleep(1)
except Exception as e:
print(e)
time.sleep(10)
ts.set_token(token)
pro = ts.pro_api()
else:
if stockholder.empty and stockfloat.empty:
return pd.DataFrame(), pd.DataFrame()
else:
return stockholder, stockfloat
# 十大 股东 流动
def insert_db(df, name, float_holder=True):
if float_holder:
insert_cmd = '''
insert into tb_sharesholder (code,name,ann_date,end_date,holder_name,hold_amount,hold_ratio)VALUES (%s,%s,%s,%s,%s,%s,%s)'''
for index, row in df.iterrows():
# print(index, row['ts_code'], row['ann_date'], row['end_date'], row['holder_name'], row['hold_amount'],
# row['hold_ratio'])
try:
cursor.execute(insert_cmd, (
row['ts_code'].split('.')[0], name, row['ann_date'], row['end_date'], row['holder_name'],
row['hold_amount'], row['hold_ratio']))
except pymysql.err.IntegrityError:
print('dup item')
conn.rollback()
continue
else:
conn.commit()
else:
insert_cmd = '''
insert into tb_sharesholder_float (code,name,ann_date,end_date,holder_name,hold_amount)VALUES (%s,%s,%s,%s,%s,%s)'''
for index, row in df.iterrows():
# print(index, row['ts_code'], row['ann_date'], row['end_date'], row['holder_name'], row['hold_amount'])
try:
cursor.execute(insert_cmd, (
row['ts_code'].split('.')[0], name, row['ann_date'], row['end_date'], row['holder_name'],
row['hold_amount']))
except pymysql.err.IntegrityError:
print('dup')
conn.rollback()
continue
else:
conn.commit()
conn.commit()
print('save successful')
def main():
code_dict = get_stock_list()
next_stock = True
check_sql = '''
SELECT code FROM `tb_sharesholder_float` GROUP BY code'''
cursor.execute(check_sql)
ret = cursor.fetchall()
result = []
for i in ret:
result.append(i[0])
for code, name in code_dict.items():
# if next_stock:
print('name :', name)
temp_code = code.split('.')[0]
if temp_code in result:
continue
start_date = '20{}0101'
end_date = '20{}1231'
for i in range(18, 0, -1):
start = start_date.format(str(i).zfill(2))
end = end_date.format(str(i).zfill(2))
df0, df1 = get_stockholder(code, start, end)
if not df0.empty and not df1.empty:
insert_db(df0, name, True)
insert_db(df1, name, False)
else:
# 换一个个股
# next_stock=False
break
def test():
# 过了时间还是有数据
code = '603259.SH'
start = '2010-01-01'
end = '2012-02-02'
stockholder = pro.top10_holders(ts_code=code, start_date=start, end_date=end)
print(stockholder)
# test()
# create_date()
# df0, df1 = get_stockholder('300333.SZ', '20180101', '20181231')
# insert_db(df0)
main()
conn.close()