forked from yfiua/index-constituents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-constituents.py
executable file
·288 lines (233 loc) · 9.5 KB
/
get-constituents.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python3
# coding: utf-8
import pandas as pd
import requests
from selectorlib import Extractor
def convert_symbol_wglh(symbol):
return symbol + 'SS' if symbol[0] == '6' else symbol + 'SZ'
def get_constituents_from_wglh(url):
selector_yml = '''
Symbol:
css: 'td small.text-secondary'
xpath: null
multiple: true
type: Text
Name:
css: 'tr td:nth-of-type(2) a'
xpath: null
multiple: true
type: Text
'''
e = Extractor.from_yaml_string(selector_yml)
headers = { 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36' }
r = requests.get(url, headers=headers)
data = e.extract(r.text)
df = pd.DataFrame(data)
df['Symbol'] = df['Symbol'].apply(convert_symbol_wglh)
return df
def get_constituents_from_slickcharts(url):
selector_yml = '''
Symbol:
css: 'tr td:nth-of-type(3) a'
xpath: null
multiple: true
type: Text
Name:
css: 'div.col-lg-7 tr td:nth-of-type(2) a'
xpath: null
multiple: true
type: Text
'''
e = Extractor.from_yaml_string(selector_yml)
headers = { 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36' }
r = requests.get(url, headers=headers)
data = e.extract(r.text)
df = pd.DataFrame(data)
return df
# 沪深300
def get_constituents_csi300():
url = 'https://wglh.com/ChinaIndicesCon/SH000300/'
return get_constituents_from_wglh(url)
# 中证500
def get_constituents_csi500():
url = 'https://wglh.com/ChinaIndicesCon/SH000905/'
return get_constituents_from_wglh(url)
# 中证1000
def get_constituents_csi1000():
url = 'https://wglh.com/ChinaIndicesCon/SH000852/'
return get_constituents_from_wglh(url)
# 上证指数
def get_constituents_sse():
url = 'https://wglh.com/ChinaIndicesCon/SH000001/'
return get_constituents_from_wglh(url)
# 深证成指
def get_constituents_szse():
url = 'https://wglh.com/ChinaIndicesCon/SZ399001/'
return get_constituents_from_wglh(url)
# NASDAQ100
def get_constituents_nasdaq100():
url = 'https://www.slickcharts.com/nasdaq100'
return get_constituents_from_slickcharts(url)
# S&P500
def get_constituents_sp500():
url = 'https://www.slickcharts.com/sp500'
return get_constituents_from_slickcharts(url)
# Dow Jones
def get_constituents_dowjones():
url = 'https://www.slickcharts.com/dowjones'
return get_constituents_from_slickcharts(url)
# DAX
def get_constituents_dax():
# convert symbol from 'SYMBOL:GR' to 'SYMBOL.DE'
def convert_symbol_dax(symbol):
return symbol[:-3] + '.DE'
selector_yml = '''
Symbol:
css: 'div.security-summary a.security-summary__ticker'
xpath: null
multiple: true
type: Text
Name:
css: 'div.security-summary a.security-summary__name'
xpath: null
multiple: true
type: Text
'''
e = Extractor.from_yaml_string(selector_yml)
url = 'https://www.bloomberg.com/quote/DAX:IND/members'
headers = { 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36' }
r = requests.get(url, headers=headers)
data = e.extract(r.text)
df = pd.DataFrame(data)
df['Symbol'] = df['Symbol'].apply(convert_symbol_dax)
return df
# Hang Seng Index
def get_constituents_hsi():
# convert symbol from 'XX:HK' to '00XX.HK'
def convert_symbol_hsi(symbol):
return symbol.rjust(7, '0').replace(':', '.')
selector_yml = '''
Symbol:
css: 'div.security-summary a.security-summary__ticker'
xpath: null
multiple: true
type: Text
Name:
css: 'div.security-summary a.security-summary__name'
xpath: null
multiple: true
type: Text
'''
e = Extractor.from_yaml_string(selector_yml)
url = 'https://www.bloomberg.com/quote/HSI:IND/members'
headers = { 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36' }
r = requests.get(url, headers=headers)
data = e.extract(r.text)
df = pd.DataFrame(data)
df['Symbol'] = df['Symbol'].apply(convert_symbol_hsi)
return df
# FTSE 100 (UKX)
def get_constituents_ftse100():
# convert symbol from 'SYMBOL:LN' to 'SYMBOL.L'
def convert_symbol_ftse100(symbol):
return symbol.replace(':LN', '.L')
selector_yml = '''
Symbol:
css: 'div.security-summary a.security-summary__ticker'
xpath: null
multiple: true
type: Text
Name:
css: 'div.security-summary a.security-summary__name'
xpath: null
multiple: true
type: Text
'''
e = Extractor.from_yaml_string(selector_yml)
url = 'https://www.bloomberg.com/quote/UKX:IND/members'
headers = { 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36' }
r = requests.get(url, headers=headers)
data = e.extract(r.text)
df = pd.DataFrame(data)
df['Symbol'] = df['Symbol'].apply(convert_symbol_ftse100)
return df
# main
if __name__ == '__main__':
print('Fetching the constituents of CSI 300...')
try:
df = get_constituents_csi300()
df.to_csv('docs/constituents-csi300.csv', index=False)
df.to_json('docs/constituents-csi300.json', orient='records')
except:
print('Failed to fetch the constituents of CSI 300.')
print('Fetching the constituents of CSI 500...')
try:
df = get_constituents_csi500()
df.to_csv('docs/constituents-csi500.csv', index=False)
df.to_json('docs/constituents-csi500.json', orient='records')
except:
print('Failed to fetch the constituents of CSI 500.')
print('Fetching the constituents of CSI 1000...')
try:
df = get_constituents_csi1000()
df.to_csv('docs/constituents-csi1000.csv', index=False)
df.to_json('docs/constituents-csi1000.json', orient='records')
except:
print('Failed to fetch the constituents of CSI 1000.')
print('Fetching the constituents of SSE...')
try:
df = get_constituents_sse()
df.to_csv('docs/constituents-sse.csv', index=False)
df.to_json('docs/constituents-sse.json', orient='records')
except:
print('Failed to fetch the constituents of SSE.')
print('Fetching the constituents of SZSE...')
try:
df = get_constituents_szse()
df.to_csv('docs/constituents-szse.csv', index=False)
df.to_json('docs/constituents-szse.json', orient='records')
except:
print('Failed to fetch the constituents of SZSE.')
print('Fetching the constituents of NASDAQ 100...')
try:
df = get_constituents_nasdaq100()
df.to_csv('docs/constituents-nasdaq100.csv', index=False)
df.to_json('docs/constituents-nasdaq100.json', orient='records')
except:
print('Failed to fetch the constituents of NASDAQ 100.')
print('Fetching the constituents of S&P 500...')
try:
df = get_constituents_sp500()
df.to_csv('docs/constituents-sp500.csv', index=False)
df.to_json('docs/constituents-sp500.json', orient='records')
except:
print('Failed to fetch the constituents of S&P 500.')
print('Fetching the constituents of Dow Jones...')
try:
df = get_constituents_dowjones()
df.to_csv('docs/constituents-dowjones.csv', index=False)
df.to_json('docs/constituents-dowjones.json', orient='records')
except:
print('Failed to fetch the constituents of Dow Jones.')
print('Fetching the constituents of DAX...')
try:
df = get_constituents_dax()
df.to_csv('docs/constituents-dax.csv', index=False)
df.to_json('docs/constituents-dax.json', orient='records')
except:
print('Failed to fetch the constituents of DAX.')
print('Fetching the constituents of Hang Seng Index...')
try:
df = get_constituents_hsi()
df.to_csv('docs/constituents-hsi.csv', index=False)
df.to_json('docs/constituents-hsi.json', orient='records')
except:
print('Failed to fetch the constituents of Hang Seng Index.')
print('Fetching the constituents of FTSE 100...')
try:
df = get_constituents_ftse100()
df.to_csv('docs/constituents-ftse100.csv', index=False)
df.to_json('docs/constituents-ftse100.json', orient='records')
except:
print('Failed to fetch the constituents of FTSE 100.')
print('Done.')