-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcurfinsto.py
executable file
·255 lines (214 loc) · 9.66 KB
/
curfinsto.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
# Copyright Rob Stainforth
#
#!/usr/bin/env python
# TMX Scraper for stock symbols and their rate of return.
# Inspired by following webpage:
# #https://stackoverflow.com/questions/8049520/web-scraping-javascript-page-with-python
#https://stackoverflow.com/questions/10309550/python-beautifulsoup-iterate-over-table
#https://stackoverflow.com/questions/37926684/entering-value-into-search-bar-and-downloading-output-from-webpage
###################################
###################################
# import dryscrape
# from bs4 import BeautifulSoup
# session = dryscrape.Session()
# session.visit(my_url)
# response = session.body()
# soup = BeautifulSoup(response)
# soup.find(id="intro-text")
## Result:
# <p id="intro-text">Yay! Supports javascript</p>
###################################
###################################
# import dryscrape
from bs4 import BeautifulSoup
import os
import sys
import json
import requests
import string
import datetime
import glob
import time
#import shutil
################################################
################################################
def get_value( value_divs ):
_value = "N/A"
for div in value_divs:
_value = "".join(((div.get_text()).split("$")[1]).split())
return _value
def get_volume( volume_divs ):
volume_value = "N/A"
for div in volume_divs:
volume_value = "".join(((div.get_text()).split(':')[1]).split())
return volume_value
def get_dividend( dividend_divs ):
dividend_value = "N/A"
for div in dividend_divs:
dividend_value = "".join(((div.get_text()).split(':')[1]).split())
print ( div )
return dividend_value
################################################
################################################
def get_variable_index( variable ):
if ( variable == "volume" ): return 0
elif ( variable == "value" ): return 1
elif ( variable == "open" ): return 2
elif ( variable == "high" ): return 3
elif ( variable == "sharesout" ): return 4
elif ( variable == "beta" ): return 5
elif ( variable == "prevclose" ): return 6
elif ( variable == "low" ): return 7
elif ( variable == "marketcap" ): return 8
elif ( variable == "vwap" ): return 9
elif ( variable == "dividend" ): return 10
elif ( variable == "divfrequency" ): return 11
elif ( variable == "peratio" ): return 12
elif ( variable == "eps" ): return 13
elif ( variable == "yield" ): return 14
elif ( variable == "exdivdate" ): return 15
elif ( variable == "pbratio" ): return 16
elif ( variable == "exchange" ): return 17
else:
print( "Error, unknown variable" )
print( "Options are: " )
print( "volume" )
print( "value" )
print( "open" )
print( "high" )
print( "sharesout" )
print( "beta" )
print( "prevclose" )
print( "low" )
print( "marketcap" )
print( "vwap" )
print( "dividend" )
print( "divfrequency" )
print( "peratio" )
print( "eps" )
print( "yield" )
print( "exdivdate" )
print( "pbratio" )
print( "exchange" )
return -1
################################################
################################################
def get_stock_info( symbol ):
stock_info = [[],[],[]]
response = ''
while response == '':
try:
response = requests.get("https://web.tmxmoney.com/quote.php?qm_symbol="+str(symbol))
break
except:
print( "Error in connection, will sleep" )
time.sleep(5)
data = response.text
soup = BeautifulSoup( response.text, "lxml" )
# Get the volume value
my_divs = soup.find_all('div', {"class": "quote-volume volumeLarge"})
volume = get_volume( my_divs )
volume = volume.replace(",","")
stock_info[1].append("Volume")
stock_info[2].append(volume)
volume = "Volume: " + str( volume )
stock_info[0].append( volume )
# Get the price value
my_values = soup.find_all('div', {"class": "quote-price priceLarge"})
value = get_value( my_values )
stock_info[1].append("Value")
stock_info[2].append(value)
value = "Value: " + str( value )
stock_info[0].append( value )
response = ''
while response == '':
try:
response = requests.get("https://web.tmxmoney.com/quote.php?qm_symbol="+str(symbol))
break
except:
print( "Error in connection, will sleep" )
time.sleep(5)
data = response.text
soup = BeautifulSoup( response.text, "lxml" )
rows = soup.find_all( "table", {"class": "detailed-quote-table"} )
for row in rows:
cols = row.find_all("tr")
for col in cols:
cells = col.find_all("td")
# Get the field name of the table value (index 0)
str_index = cells[0].get_text()
str_index = "".join(str_index.split())
# Keys with "." in name mess up mongodb
str_index = str_index.replace(".", "")
# Removing "/" just in case
# str_index = str_index.replace("/", "")
# The value of the field (index 1)
str_value = cells[1].get_text()
# strips whitespace from the string
str_value = "".join(str_value.split())
# Removes commas from numbers.
str_value = str_value.replace(",", "")
# Print the information.
cur_out = str_index + " " + str_value
stock_info[1].append( str_index.replace(":", "") )
stock_info[2].append( str_value )
stock_info[0].append( cur_out )
# for info in stock_info:
# print( info )
# for x in range( 0, len(stock_info) ):
# tmp_str = stock_info[x].encode('ascii')
# stock_info[x] = tmp_str
# for info in stock_info:
# print( info )
return stock_info
################################################
################################################
def get_stocks_by_letter( my_letter, out_file ):
alphabet = []
names = []
symbols = []
for letter in range(65, 91):
alphabet.append(chr(letter))
for alpha in range( 0,len(alphabet) ):
if ( my_letter != alphabet[alpha] ): continue
response = ''
while response == '':
try:
response = requests.get("https://www.tsx.com/json/company-directory/search/tsx/"+str(alphabet[alpha])+"?")
print( "Visiting: " + "https://www.tsx.com/json/company-directory/search/tsx/"+str(alphabet[alpha])+"?" )
break
except:
print( "Error in connection, will sleep" )
time.sleep(5)
data = response.text
d = json.loads(str(data))
for sym in range( len(d['results']) ):
curName = str(d['results'][sym]['name'])
names.append( curName )
curSym = str(d['results'][sym]['symbol'])
symbols.append( curSym )
with open( out_file, 'w' ) as out_f:
#for i in range( 0, len( names ) ):
for i in range( 0, 1 ):
print( "Getting info for stock: " + str(symbols[ i ]) + ", " + str(i) + "/" + str(len(names)) )
cur_info = get_stock_info( str(symbols[ i ]) )[0]
out_f.write( names[ i ] + " | " + symbols[ i ] + " | " )
#print( names[ i ] )
#print( symbols[ i ] )
for v in range( 0, len( cur_info ) ):
#print( str(cur_info[ v ]) + " | " )
#out_f.write( cur_info[ v ] )
out_f.write( cur_info[ v ] + " | " )
out_f.write( "\n" );
out_f.close()
documents = []
for i in range( 0, len( names ) ):
print( "Create document for stock: " + str(symbols[ i ]) + ", " + str(i) + "/" + str(len(names)) )
cur_info = get_stock_info( str(symbols[ i ]) )
document = {"Name": names[ i ], "Symbol": symbols[ i ]}
document["ScrapeDate"] = datetime.datetime.utcnow()
for v in range( 0, len( cur_info[0] ) ):
document[ cur_info[ 1 ][ v ] ] = cur_info[ 2 ][ v ]
#print( document )
documents.append( document )
return symbols, documents