-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.py
167 lines (151 loc) · 8.05 KB
/
scrape.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
#!/usr/bin/python3
import requests
from bs4 import BeautifulSoup
import csv
import lxml
import time
import re
import multiprocessing
from requests.adapters import HTTPAdapter
start_time = time.time()
start_day = '01'
end_day = '31'
month = '05'
year = '2019'
params = {
'search_text': 'INFORMATION TABLE',
'sort': 'Date',
'startDoc': '1',
'formType': '',
'isAdv': 'true',
'stemming': 'true',
'numResults': '10',
'fromDate': month+'/'+start_day+'/'+year,
'toDate': month+'/'+end_day+'/'+year
}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
all_urls = []
records = []
def sort_second(val):
return val[1]
def scrape_page(url):
time.sleep(5)
data = []
try:
response = requests_session.get(url, timeout=10)
soup = BeautifulSoup(response.text, 'lxml')
# testing limitations to number requests that can be made
if 'blocked' in response.text:
print('we have been blocked')
# if response of ok
if response.status_code == 200:
# for every filing on page
for filing in soup.find(attrs={'xmlns:autn': 'http://schemas.autonomy.com/aci/'}).findAll('tr', attrs={'class': None}):
# if filing is an information table (what we are looking for)
if 'INFORMATION TABLE' in filing.find(attrs={'class': 'filing'}).text:
date = filing.find(attrs={'class': 'blue'}).text
text = filing.find(attrs={'class': 'filing'}).text
name = text[text.index('for')+4:]
href = filing.find(attrs={'class': 'filing'})['href']
# fixing special cases where xml files were named incorrectly
if 'xml' in href:
link = href[href.index('http'):href.index(".xml'")+4]
# print(link)
else:
continue
try:
# get information table for scraping
info_table_response = requests_session.get(link, timeout=11)
time.sleep(5)
info_table = BeautifulSoup(
info_table_response.text, 'lxml-xml')
# test for blocked by server status
if 'blocked' in info_table_response.text:
print('we have been blocked')
# print(info_table_response.status_code)
# if response of ok
if info_table_response.status_code == 200:
total = 0
companies = []
try:
# get data for company
for row in info_table.find(attrs={'summary': 'Form 13F-NT Header Information'}).findAll('tr'):
if row.find(attrs={'class': 'FormData'}):
company = row.find(attrs={'class': 'FormData'}).text
company = company.strip()
value = row.find(
attrs={'class': 'FormDataR'}).text.replace(',', '')
total += int(value)
companies.append([company, int(value)])
companies.sort(key=sort_second, reverse=True)
for i in range(1, 11):
if len(companies) < i:
companies.append(['', ''])
output = [name, date, total]
for i in range(0, 10):
output.append(companies[i][0])
for i in range(0, 10):
output.append(companies[i][1])
data.append(output)
except:
# if error finding information table
data.append([name, date, total, 'Information Table Not Found'])
except requests.exceptions.ConnectionError as e:
data.append([name, date, '', 'Information Table Not Found: Connection error, make sure you are connected to the internet'])
print(e)
except requests.exceptions.Timeout as e:
data.append([name, date, '', 'Information Table Not Found: Timeout Error'])
print(e)
except requests.exceptions.RequestException as e:
data.append([name, date, '', 'Information Table Not Found: Error'])
print(e)
except KeyboardInterrupt:
data.append([name, date, '', 'Information Table Not Found: Someone stopped the program'])
except requests.exceptions.ConnectionError as e:
print('ERROR:', url)
print(e)
except requests.exceptions.Timeout as e:
print('ERROR: ', url)
print(e)
except requests.exceptions.RequestException as e:
print('ERROR: ', url)
print(e)
except KeyboardInterrupt as e:
print('ERROR: ', url)
print(e)
return data
# generate all the urls to scrape based on how many pages of results there are
def generate_urls(form_type):
params['formType'] = form_type
FHR_response = requests_session.get(
'https://searchwww.sec.gov/EDGARFSClient/jsp/EDGAR_MainAccess.jsp', params=params)
FHR = BeautifulSoup(FHR_response.text, 'lxml')
if FHR_response.status_code == 200:
# print(FHR.find('table', id='result'))
# print(FHR.find('table', id='result').find(text=re.compile('results')))
if(FHR.find('table', id='result').find(text=re.compile('results'))):
total_results = int(FHR.find('table', id='result').find(text=re.compile('results')).parent.previous_sibling.text)
for i in range(1,total_results,10):
all_urls.append('https://searchwww.sec.gov/EDGARFSClient/jsp/EDGAR_MainAccess.jsp?search_text=INFORMATION+TABLE&sort=Date&startDoc='+str(i)+'&numResults=10&isAdv=true&formType='+form_type+'&fromDate='+month+'/'+start_day+'/'+year+'&toDate='+month+'/'+end_day+'/'+year+'&stemming=true')
# max the number of attempts made at 5
requests_session = requests.Session()
requests_session.mount('www.sec.gov', HTTPAdapter(max_retries=5))
requests_session.mount('http://', HTTPAdapter(max_retries=5))
requests_session.mount('https://', HTTPAdapter(max_retries=5))
# find all urls to scrape for each form type
generate_urls('Form13FHR')
generate_urls('Form13FHRAD')
# multiprocessing threads = 5
if __name__ == '__main__':
with multiprocessing.Pool(5) as p:
records = p.map(scrape_page, all_urls)
# write out data to funds.csv
with open('funds.csv', 'w+', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
headers = ['Name', 'Filing Date', 'Total Assets', '1 Name', '2 Name', '3 Name', '4 Name', '5 Name', '6 Name', '7 Name', '8 Name',
'9 Name', '10 Name', '1 Value', '2 Value', '3 Value', '4 Value', '5 Value', '6 Value', '7 Value', '8 Value', '9 Value', '10 Value']
csv_writer.writerow(headers)
for page in records:
csv_writer.writerows(page)
print(multiprocessing.current_process().name)
print("--- %s seconds ---" % (time.time() - start_time))