-
Notifications
You must be signed in to change notification settings - Fork 1
/
EdgarScrape.py
495 lines (461 loc) · 20.1 KB
/
EdgarScrape.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import os
import re
import urllib
import requests
from bs4 import BeautifulSoup as BS
from tqdm import tqdm
import LinkURL
import settings
class GetFilings:
def __init__(self, ticker_symbol):
self.ticker_symbol = ticker_symbol
self.filings = {
'10q_list': [],
'10q_xl_list': [],
'10q_xml': [],
'10q_html': [],
'10q_txt': [],
'10q_xl': [],
'10k_list': [],
'10k_xl_list': [],
'10k_xml': [],
'10k_html': [],
'10k_txt': [],
'10k_xl': [],
'success': {
'count': 0,
'10-Q': None,
'10-K': None
},
'errors': {
'count': 0,
'10-Q': None,
'10-K': None
}
}
print('Scraping {0}'.format(self.ticker_symbol))
print('Getting 10-Q list...')
self.get_10q_list()
print('Getting 10-K list...')
self.get_10k_list()
print('Getting 10-Q files...')
self.get_all_10q()
print('Getting 10-K files...')
self.get_all_10k()
print('Downloading all files...')
self.download_all()
def create_folders(self):
if not os.path.exists('{0}/{1}/'.format(settings.RAW_DATA_PATH, self.ticker_symbol)):
os.makedirs(
'{0}/{1}/'.format(settings.RAW_DATA_PATH, self.ticker_symbol))
if settings.GET_XML:
if not os.path.exists('{0}/{1}/xml/10-Q/'.format(settings.RAW_DATA_PATH, self.ticker_symbol)):
os.makedirs(
'{0}/{1}/xml/10-Q/'.format(settings.RAW_DATA_PATH, self.ticker_symbol))
if not os.path.exists('{0}/{1}/xml/10-K/'.format(settings.RAW_DATA_PATH, self.ticker_symbol)):
os.makedirs(
'{0}/{1}/xml/10-K/'.format(settings.RAW_DATA_PATH, self.ticker_symbol))
if settings.GET_HTML:
if not os.path.exists('{0}/{1}/html/10-Q/'.format(settings.RAW_DATA_PATH, self.ticker_symbol)):
os.makedirs(
'{0}/{1}/html/10-Q/'.format(settings.RAW_DATA_PATH, self.ticker_symbol))
if not os.path.exists('{0}/{1}/html/10-K/'.format(settings.RAW_DATA_PATH, self.ticker_symbol)):
os.makedirs(
'{0}/{1}/html/10-K/'.format(settings.RAW_DATA_PATH, self.ticker_symbol))
if settings.GET_TXT:
if not os.path.exists('{0}/{1}/txt/10-K/'.format(settings.RAW_DATA_PATH, self.ticker_symbol)):
os.makedirs(
'{0}/{1}/txt/10-K/'.format(settings.RAW_DATA_PATH, self.ticker_symbol))
if not os.path.exists('{0}/{1}/txt/10-Q/'.format(settings.RAW_DATA_PATH, self.ticker_symbol)):
os.makedirs(
'{0}/{1}/txt/10-Q/'.format(settings.RAW_DATA_PATH, self.ticker_symbol))
if settings.GET_XL:
if not os.path.exists('{0}/{1}/xl/10-K/'.format(settings.RAW_DATA_PATH, self.ticker_symbol)):
os.makedirs(
'{0}/{1}/xl/10-K/'.format(settings.RAW_DATA_PATH, self.ticker_symbol))
if not os.path.exists('{0}/{1}/xl/10-Q/'.format(settings.RAW_DATA_PATH, self.ticker_symbol)):
os.makedirs(
'{0}/{1}/xl/10-Q/'.format(settings.RAW_DATA_PATH, self.ticker_symbol))
def validate_page(self, html):
try:
check = html.find_all('center')[0].text
except IndexError:
self.create_folders()
return True
return False
def get_main_html(self, q_or_k):
link = settings.LINK_URL.format(self.ticker_symbol, q_or_k)
r = requests.get(link)
s = BS(r.text, "lxml")
if self.validate_page(s):
return s
else:
return False
def get_10q_list(self):
html = self.get_main_html('10-Q')
if html:
for link in html.find_all('a', {'id': 'documentsbutton'}):
doc_url = 'https://www.sec.gov' + link['href']
self.filings['10q_list'].append(doc_url)
for link in html.find_all('a', {'id': 'interactiveDataBtn'}):
doc_url = 'https://www.sec.gov' + link['href']
self.filings['10q_xl_list'].append(doc_url)
else:
return False
def get_10k_list(self):
html = self.get_main_html('10-K')
if html:
for link in html.find_all('a', {'id': 'documentsbutton'}):
doc_url = 'https://www.sec.gov' + link['href']
self.filings['10k_list'].append(doc_url)
for link in html.find_all('a', {'id': 'interactiveDataBtn'}):
doc_url = 'https://www.sec.gov' + link['href']
self.filings['10k_xl_list'].append(doc_url)
def get_xml_file(self, html, i):
s = BS(html, "lxml")
try:
xml_link = s.find_all('table', {'class': 'tableFile', 'summary': 'Data Files'})[
0].find_all('tr')[i].find_all('td')[2].find('a')['href']
xtname = ['.xml', '.xsd']
if os.path.splitext(xml_link)[1] in xtname:
xml_link = 'https://www.sec.gov' + xml_link
return xml_link
else:
return False
except IndexError:
return False
def get_html(self, html):
s = BS(html, "lxml")
try:
html_link = s.find_all('table', {'class': 'tableFile', 'summary': 'Document Format Files'})[
0].find_all('tr')[1].find('a')['href']
xtname = ['.html', '.htm']
if os.path.splitext(html_link)[1] in xtname:
html_link = 'https://www.sec.gov' + html_link
return html_link
else:
return False
except IndexError:
return False
# TODO DOnt need this
def get_txt(self, html):
s = BS(html, "lxml")
try:
txt_link = s.find_all('table', {
'class': 'tableFile', 'summary': 'Document Format Files'})[0].find_all('td')
link_idx = None
for idx, val in enumerate(txt_link):
if 'Complete submission' in val.text:
link_idx = idx + 1
txt_link = txt_link[link_idx].find('a')['href']
xtname = ['.txt']
if os.path.splitext(txt_link)[1] in xtname:
txt_link = 'https://www.sec.gov' + txt_link
return txt_link
else:
return False
except IndexError:
return False
def get_xl(self, html):
s = BS(html)
try:
xl_link = s.find_all('td', {'colspan': '2'})[
0].find_all('a')[1]['href']
link = 'https://www.sec.gov' + xl_link
return link
except IndexError:
return False
# TODO Fix this
def get_date(self, html):
# TODO Fix this unnecessary double calling of the soup
s = BS(html, "lxml")
try:
date = s.find_all('div', {'class': 'formGrouping'})[
1].find_all('div')[1].text
return date
except (IndexError, AttributeError):
return False
# TODO Dont need any of this
def get_all_10q(self):
if len(self.filings['10q_list']) == 0:
try:
self.filings['errors']['10-Q'].append('all')
except (KeyError, AttributeError):
self.filings['errors']['10-Q'] = []
self.filings['errors']['10-Q'].append('all')
return False
dates = []
errors = {}
success = {}
for link_val in tqdm(self.filings['10q_list'], desc="10-Q: HTML"):
# print('requesting from: ' + str(link_val))
r = requests.get(link_val)
html_txt = r.text
# TODO Here get rid of this
date = self.get_date(html_txt) # SLOW
dates.append(date)
success[date] = []
# TODO DOnt need this
if settings.GET_XML:
for i in range(1, 7):
link_xml = self.get_xml_file(html_txt, i)
if link_xml:
self.filings['10q_xml'].append((link_xml, date))
try:
success[date].append((link_xml, date))
except (KeyError, AttributeError):
success[date] = []
success[date].append('xml')
elif not link_xml:
try:
errors[date].append('xml')
except (KeyError, AttributeError):
errors[date] = []
errors[date].append('xml')
if settings.GET_HTML:
link_html = self.get_html(html_txt)
if link_html:
self.filings['10q_html'].append((link_html, date, 'html'))
try:
success[date].append('html')
except (KeyError, AttributeError):
success[date] = []
success[date].append('html')
elif not link_html:
try:
errors[date].append('html')
except (KeyError, AttributeError):
errors[date] = []
errors[date].append('html')
# TODO Dont need this
if settings.GET_TXT:
link_txt = self.get_txt(html_txt)
if link_txt:
self.filings['10q_txt'].append((link_txt, date, 'txt'))
try:
success[date].append('txt')
except (KeyError, AttributeError):
success[date] = []
success[date].append('txt')
elif not link_txt:
try:
errors[date].append('txt')
except (KeyError, AttributeError):
errors[date] = []
errors[date].append('txt')
# TODO Dont need this either
if settings.GET_XL:
count = 0
for link_val in self.filings['10q_xl_list']:
r = requests.get(link_val)
html_txt = r.text
link_xl = self.get_xl(html_txt)
if link_xl:
self.filings['10q_xl'].append(
(link_xl, dates[count], 'xl'))
try:
success[dates[count]].append('xl')
except (KeyError, AttributeError):
success[dates[count]] = []
success[dates[count]].append('xl')
elif not link_xl:
try:
errors[dates[count]].append('xl')
except (KeyError, AttributeError):
errors[dates[count]] = []
errors[dates[count]].append('xl')
count += 1
if len(errors) > 0:
self.filings['errors']['count'] += 1
self.filings['errors']['10-Q'] = errors
if len(success) > 0:
self.filings['success']['count'] += 1
self.filings['success']['10-Q'] = success
def get_all_10k(self):
if len(self.filings['10k_list']) == 0:
try:
self.filings['errors']['10-K'].append('all')
except (KeyError, AttributeError):
self.filings['errors']['10-K'] = []
self.filings['errors']['10-K'].append('all')
return False
dates = []
errors = {}
success = {}
for link_val in tqdm(self.filings['10k_list'], desc="10_klist"):
r = requests.get(link_val)
html_txt = r.text
date = self.get_date(html_txt)
dates.append(date)
success[date] = []
if settings.GET_XML:
for i in range(1, 7):
link_xml = self.get_xml_file(html_txt, i)
if link_xml:
self.filings['10k_xml'].append((link_xml, date))
try:
success[date].append((link_xml, date))
except (KeyError, AttributeError):
success[date] = []
success[date].append('xml')
elif not link_xml:
try:
errors[date].append('xml')
except (KeyError, AttributeError):
errors[date] = []
errors[date].append('xml')
if settings.GET_HTML:
link_html = self.get_html(html_txt)
if link_html:
self.filings['10k_html'].append((link_html, date, 'html'))
try:
success[date].append('html')
except (KeyError, AttributeError):
success[date] = []
success[date].append('html')
elif not link_html:
try:
errors[date].append('html')
except (KeyError, AttributeError):
errors[date] = []
errors[date].append('html')
if settings.GET_TXT:
link_txt = self.get_txt(html_txt)
if link_txt:
self.filings['10k_txt'].append((link_txt, date, 'txt'))
try:
success[date].append('txt')
except (KeyError, AttributeError):
success[date] = []
success[date].append('txt')
elif not link_txt:
try:
errors[date].append('txt')
except (KeyError, AttributeError):
errors[date] = []
errors[date].append('txt')
if settings.GET_XL:
count = 0
for link_val in self.filings['10k_xl_list']:
r = requests.get(link_val)
html_txt = r.text
link_xl = self.get_xl(html_txt)
if link_xl:
self.filings['10k_xl'].append(
(link_xl, dates[count], 'xl'))
try:
success[dates[count]].append('xl')
except (KeyError, AttributeError):
success[dates[count]] = []
success[dates[count]].append('xl')
elif not link_xl:
try:
errors[dates[count]].append('xl')
except (KeyError, AttributeError):
errors[dates[count]] = []
errors[dates[count]].append('xl')
count += 1
if len(errors) > 0:
self.filings['errors']['count'] += 1
self.filings['errors']['10-K'] = errors
if len(success) > 0:
self.filings['success']['count'] += 1
self.filings['success']['10-K'] = success
def check_duplicate(self, directory, filename):
if filename in os.listdir(directory):
return True
return False
def download_all(self):
# Download XML files
if settings.GET_XML:
for link in self.filings['10q_xml']:
print(link)
fname = os.path.split(link[0])[1]
ext = os.path.splitext(link[0])[1]
fname = "{0}_{1}{2}".format(self.ticker_symbol, fname, ext)
diry = '{0}/{1}/xml/10-Q/{2}/'.format(
settings.RAW_DATA_PATH, self.ticker_symbol, link[1])
if not os.path.exists(diry):
os.makedirs(diry)
if not self.check_duplicate(diry, fname):
urllib.request.urlretrieve(
link[0], '{0}{1}'.format(diry, fname))
for link in self.filings['10k_xml']:
fname = os.path.split(link[0])[1]
ext = os.path.splitext(link[0])[1]
fname = "{0}_{1}{2}".format(self.ticker_symbol, fname, ext)
diry = '{0}/{1}/xml/10-K/{2}/'.format(
settings.RAW_DATA_PATH, self.ticker_symbol, link[1])
if not os.path.exists(diry):
os.makedirs(diry)
if not self.check_duplicate(diry, fname):
urllib.request.urlretrieve(
link[0], '{0}{1}'.format(diry, fname))
# Download HTML files
if settings.GET_HTML:
for link in tqdm(self.filings['10q_html'], desc="10-Q"):
fname = "{0}_{1}_{2}.html".format(
self.ticker_symbol, link[1], link[2])
diry = '{0}/{1}/html/10-Q/{2}/'.format(
settings.RAW_DATA_PATH, self.ticker_symbol, link[1])
if not os.path.exists(diry):
os.makedirs(diry)
if not self.check_duplicate(diry, fname):
urllib.request.urlretrieve(
link[0], '{0}{1}'.format(diry, fname))
for link in tqdm(self.filings['10k_html'], desc="10-K"):
fname = "{0}_{1}_{2}.html".format(
self.ticker_symbol, link[1], link[2])
diry = '{0}/{1}/html/10-K/{2}/'.format(
settings.RAW_DATA_PATH, self.ticker_symbol, link[1])
if not os.path.exists(diry):
os.makedirs(diry)
if not self.check_duplicate(diry, fname):
urllib.request.urlretrieve(
link[0], '{0}{1}'.format(diry, fname))
# Download TXT files
if settings.GET_TXT:
for link in self.filings['10q_txt']:
fname = "{0}_{1}_{2}.txt".format(
self.ticker_symbol, link[1], link[2])
diry = '{0}/{1}/txt/10-Q/{2}/'.format(
settings.RAW_DATA_PATH, self.ticker_symbol, link[1])
if not os.path.exists(diry):
os.makedirs(diry)
if not self.check_duplicate(diry, fname):
urllib.request.urlretrieve(
link[0], '{0}{1}'.format(diry, fname))
for link in self.filings['10k_txt']:
fname = "{0}_{1}_{2}.txt".format(
self.ticker_symbol, link[1], link[2])
diry = '{0}/{1}/txt/10-K/{2}/'.format(
settings.RAW_DATA_PATH, self.ticker_symbol, link[1])
if not os.path.exists(diry):
os.makedirs(diry)
if not self.check_duplicate(diry, fname):
urllib.request.urlretrieve(
link[0], '{0}{1}'.format(diry, fname))
# Download XL files
if settings.GET_XL:
for link in self.filings['10q_xl']:
fname = "{0}_{1}_{2}.xlsx".format(
self.ticker_symbol, link[1], link[2])
fname = self.ticker_symbol + fname
diry = '{0}/{1}/xl/10-Q/{2}/'.format(
settings.RAW_DATA_PATH, self.ticker_symbol, link[1])
if not os.path.exists(diry):
os.makedirs(diry)
if not self.check_duplicate(diry, fname):
urllib.request.urlretrieve(
link[0], '{0}{1}'.format(diry, fname))
for link in self.filings['10k_xl']:
fname = "{0}_{1}_{2}.xlsx".format(
self.ticker_symbol, link[1], link[2])
fname = self.ticker_symbol + fname
diry = '{0}/{1}/xl/10-K/{2}/'.format(
settings.RAW_DATA_PATH, self.ticker_symbol, link[1])
if not os.path.exists(diry):
os.makedirs(diry)
if not self.check_duplicate(diry, fname):
urllib.request.urlretrieve(
link[0], '{0}{1}'.format(diry, fname))