Skip to content

Commit 7a35b01

Browse files
committed
[fix] update EastSpider
1 parent 9f0793d commit 7a35b01

1 file changed

Lines changed: 74 additions & 20 deletions

File tree

spider.py

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,92 @@ def get_today_list(self) -> list:
2323
pass
2424

2525

26+
# Outdated Spider
27+
# class EastSpider(BaseSpider):
28+
# def __init__(self, ps='50', url='http://dcfm.eastmoney.com/em_mutisvcexpandinterface/api/js/get'):
29+
# super(EastSpider, self).__init__()
30+
# self.url = url
31+
# self.params = {
32+
# 'type': 'KZZ_LB2.0',
33+
# 'token': '',
34+
# 'cmd': '',
35+
# 'st': 'STARTDATE',
36+
# 'sr': '-1',
37+
# 'p': '1',
38+
# 'ps': ps # page_size
39+
# }
40+
#
41+
# def get_list(self):
42+
# self.params['token'] = self._get_token()
43+
# r = requests.get(self.url, params=self.params, headers=headers)
44+
# return json.loads(r.text)
45+
#
46+
# def get_today_list(self) -> list:
47+
# r_list = self.get_list()
48+
# today_str = time.strftime("%Y-%m-%d", time.localtime())
49+
# today_list = []
50+
# for stock in r_list:
51+
# if stock['STARTDATE'].split('T')[0] == today_str:
52+
# today_list.append([stock['CORRESCODE'], stock['SNAME']])
53+
# return today_list
54+
#
55+
# def _get_token(self) -> str:
56+
# token_url = 'http://data.eastmoney.com/kzz/default.html'
57+
# r = requests.get(token_url, headers=headers)
58+
# token_list = re.findall('&token=(.+?)&', r.text)
59+
# if len(token_list) != 1:
60+
# raise ValueError("Token获取出错!")
61+
# return token_list[0]
62+
63+
# New Example GET request:
64+
# https://datacenter-web.eastmoney.com/api/data/v1/get
65+
# ?sortColumns=PUBLIC_START_DATE
66+
# &sortTypes=-1
67+
# &pageSize=50
68+
# &pageNumber=1
69+
# &reportName=RPT_BOND_CB_LIST
70+
# &columns=ALL
71+
2672
class EastSpider(BaseSpider):
27-
def __init__(self, ps='10', url='http://dcfm.eastmoney.com/em_mutisvcexpandinterface/api/js/get'):
73+
# 新东方财富网API移除了token验证字段,感谢!
74+
def __init__(self, url='https://datacenter-web.eastmoney.com/api/data/v1/get'):
2875
super(EastSpider, self).__init__()
2976
self.url = url
3077
self.params = {
31-
'type': 'KZZ_LB2.0',
32-
'token': '',
33-
'cmd': '',
34-
'st': 'STARTDATE',
35-
'sr': '-1',
36-
'p': '1',
37-
'ps': ps # page_size
78+
'sortColumns': 'PUBLIC_START_DATE',
79+
'sortTypes': '-1',
80+
'pageSize': '10',
81+
'pageNumber': '1',
82+
'reportName': 'RPT_BOND_CB_LIST',
83+
'columns': 'ALL',
84+
'source': 'WEB',
85+
'client': 'WEB'
3886
}
3987

4088
def get_list(self):
41-
self.params['token'] = self._get_token()
4289
r = requests.get(self.url, params=self.params, headers=headers)
43-
return json.loads(r.text)
90+
ret_dict = json.loads(r.text)
91+
try:
92+
data: list = ret_dict['result']['data']
93+
return data
94+
except KeyError:
95+
raise RuntimeError('API解析错误')
4496

4597
def get_today_list(self) -> list:
46-
r_list = self.get_list()
98+
try:
99+
r_list = self.get_list()
100+
except RuntimeError as e: # API偶尔解析错误,直接返回空list
101+
print(str(e))
102+
return []
47103
today_str = time.strftime("%Y-%m-%d", time.localtime())
104+
# today_str = '2021-07-15'
48105
today_list = []
49106
for stock in r_list:
50-
if stock['STARTDATE'].split('T')[0] == today_str:
51-
today_list.append([stock['CORRESCODE'], stock['SNAME']])
107+
if stock['VALUE_DATE'].split(' ')[0] == today_str:
108+
today_list.append([stock['CORRECODE'], stock['SECURITY_NAME_ABBR']])
52109
return today_list
53110

54-
def _get_token(self) -> str:
55-
token_url = 'http://data.eastmoney.com/kzz/default.html'
56-
r = requests.get(token_url, headers=headers)
57-
token_list = re.findall('&token=(.+?)&', r.text)
58-
if len(token_list) != 1:
59-
raise ValueError("Token获取出错!")
60-
return token_list[0]
111+
112+
if __name__ == '__main__':
113+
spider = EastSpider()
114+
print(spider.get_today_list())

0 commit comments

Comments
 (0)