forked from TsmileAssassin/stock_discover
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathguoren_api.py
156 lines (122 loc) · 7 KB
/
guoren_api.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
import json
import time
import urllib.request
from pandas import Series
from user_define_cookie import UserDefineCookie
class GuorenApi(object):
def __init__(self, symbol=None):
self.symbol = symbol
self.__req_url = 'https://guorn.com/stock/query'
self.__req_delay = 5
def get_req_headers(self):
return {
'Connection': 'keep-alive',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Origin': 'https://guorn.com',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
'Referer': 'https://guorn.com/stock/history?his=1&ticker={},0.M.%E8%82%A1%E7%A5%A8%E6%AF%8F%E6%97%A5%E6%8C%87%E6%A0%87_%E5%B8%82%E7%9B%88%E7%8E%87.0,1'.format(
self.symbol),
'Content-Type': 'application/json; charset=UTF-8',
'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6',
'Cookie': UserDefineCookie.get_guoren_cookie()
}
def get_req_url(self):
return self.__req_url
def get_req_delay(self):
return self.__req_delay
def get_req_data_pe(self):
return '{"ticker":[["%s","0.M.股票每日指标_市盈率.0"]],"index":[],"sector":[],"pool":[],"strategy":[]}' % self.symbol
def get_req_data_pb(self):
return '{"ticker":[["%s","0.M.股票每日指标_市净率.0"]],"index":[],"sector":[],"pool":[],"strategy":[]}' % self.symbol
def get_req_data_volume(self):
return '{"ticker":[["%s","0.M.股票每日指标_5日均成交量.0"]],"index":[],"sector":[],"pool":[],"strategy":[]}' % self.symbol
def get_req_data_gross(self):
return '{"ticker":[["%s","0.M.股票每日指标_销售毛利率.0"]],"index":[],"sector":[],"pool":[],"strategy":[]}' % self.symbol
def get_req_data_interest(self):
return '{"ticker":[["%s","0.M.股票每日指标_销售净利率.0"]],"index":[],"sector":[],"pool":[],"strategy":[]}' % self.symbol
def get_req_data_dy(self):
return '{"ticker":[["%s","0.M.股票每日指标_股息率.0"]],"index":[],"sector":[],"pool":[],"strategy":[]}' % self.symbol
def get_req_data_roe(self):
return '{"ticker":[["%s","0.M.股票每日指标_资权益回报率.0"]],"index":[],"sector":[],"pool":[],"strategy":[]}' % self.symbol
def get_req_data_income_grow(self):
return '{"ticker":[["%s","0.M.股票每日指标_营业收入同比增长.0"]],"index":[],"sector":[],"pool":[],"strategy":[]}' % self.symbol
def get_req_data_profie_grow(self):
return '{"ticker":[["%s","0.M.股票每日指标_净利润增长率.0"]],"index":[],"sector":[],"pool":[],"strategy":[]}' % self.symbol
def get_req_data_gross_profie_grow(self):
return '{"ticker":[["%s","0.M.股票每日指标_毛利润增长率.0"]],"index":[],"sector":[],"pool":[],"strategy":[]}' % self.symbol
def get_req_data_turnover(self):
return '{"ticker":[["%s","0.M.股票每日指标_总资产周转率.0"]],"index":[],"sector":[],"pool":[],"strategy":[]}' % self.symbol
def submit_req_pe(self):
return self.__submit_req(self.get_req_data_pe())
def submit_req_pb(self):
return self.__submit_req(self.get_req_data_pb())
def submit_req_volume(self):
return self.__submit_req(self.get_req_data_volume())
def submit_req_gross(self):
return self.__submit_req(self.get_req_data_gross())
def submit_req_interest(self):
return self.__submit_req(self.get_req_data_interest())
def submit_req_dy(self):
return self.__submit_req(self.get_req_data_dy())
def submit_req_roe(self):
return self.__submit_req(self.get_req_data_roe())
def submit_req_income_grow(self):
return self.__submit_req(self.get_req_data_income_grow())
def submit_req_profie_grow(self):
return self.__submit_req(self.get_req_data_profie_grow())
def submit_req_gross_profie_grow(self):
return self.__submit_req(self.get_req_data_gross_profie_grow())
def submit_req_turnover(self):
return self.__submit_req(self.get_req_data_turnover())
def __submit_req(self, req_data):
time.sleep(self.get_req_delay())
guoren_data = req_data.encode("utf8")
guoren_data_len = len(guoren_data)
guoren_headers = self.get_req_headers()
guoren_headers['Content-Length'] = str(guoren_data_len)
guoren_req = urllib.request.Request(self.get_req_url(),
data=guoren_data,
headers=guoren_headers, method='POST')
guoren_content = urllib.request.urlopen(guoren_req).read().decode("utf8")
return guoren_content
@staticmethod
def parse_json_to_series(guoren_content, item_value_change_func=None, reserved_count=1800):
guoren_data = json.loads(guoren_content)
axis_data = guoren_data['data']['sheet_data']['meas_data'][0]
axis_index = guoren_data['data']['sheet_data']['row'][0]['data'][1]
axis_index = list(map(lambda x: x[2:], axis_index))
if reserved_count < len(axis_data) == len(axis_index):
end_index = len(axis_data)
axis_data = axis_data[end_index - reserved_count:end_index:1]
axis_index = axis_index[end_index - reserved_count:end_index:1]
if item_value_change_func is not None:
axis_data = list(map(item_value_change_func, axis_data))
return Series(data=axis_data, index=axis_index)
@staticmethod
def parse_json_to_series_filter_dirty(guoren_content, item_change_func, reserved_count=1800):
guoren_data = json.loads(guoren_content)
axis_data = guoren_data['data']['sheet_data']['meas_data'][0]
axis_index = guoren_data['data']['sheet_data']['row'][0]['data'][1]
axis_index = list(map(lambda x: x[2:], axis_index))
axis_tuple_list = list(zip(axis_index, axis_data))
if reserved_count < len(axis_tuple_list):
end_index = len(axis_tuple_list)
axis_tuple_list = axis_tuple_list[end_index - reserved_count:end_index:1]
axis_tuple_list = list(filter(lambda x: x[1] != '', axis_tuple_list))
axis_tuple_list = list(map(item_change_func, axis_tuple_list))
axis_unzip_tuple_list = list(zip(*axis_tuple_list))
return Series(data=list(axis_unzip_tuple_list[1]), index=list(axis_unzip_tuple_list[0]))
if __name__ == '__main__':
guoren_api = GuorenApi('601166')
print('url:' + guoren_api.get_req_url())
data = guoren_api.get_req_data_volume().encode("utf8")
data_len = len(data)
headers = guoren_api.get_req_headers()
headers['Content-Length'] = str(data_len)
print('header:' + headers.__repr__())
guoren_req = urllib.request.Request(guoren_api.get_req_url(),
data=data,
headers=headers, method='POST')
content = urllib.request.urlopen(guoren_req).read().decode("utf8")
print(content)