-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzacks_handler.py
99 lines (75 loc) · 2.74 KB
/
zacks_handler.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
import pickle
import time
import requests
from datetime import datetime
from bs4 import BeautifulSoup
def get(symbol):
try:
header = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:32.0) Gecko/20100101 Firefox/32.0'}
url = 'https://www.zacks.com/stock/research/%s/all-news/zacks' % symbol
r = requests.get(url, headers=header)
soup = BeautifulSoup(r.text, 'html.parser')
rank = soup.select_one('.rank_view')
if not rank:
return "", ""
rank = rank.text.strip()[:5]
# Still rank empty..
if not rank:
return "", ""
listitem = soup.select_one(".listitempage .listitem .byline time")
# We have a rank, but no news. That's ok. (Note: Probably won't happen..)
if not listitem:
return rank, ""
listitem = listitem.string
listitem = listitem[13:].strip()
datetime_object = datetime.strptime(listitem, '%B %d,%Y')
return rank, datetime_object
except Exception as ex:
print('ERR', symbol, ex)
return "", ""
def get_all_first_pass():
symbols = get_all_base_symbols()
new_symbols_full = []
new_symbols = []
for symbol in symbols:
rank, dt = get(symbol)
if rank != "":
new_symbols.append(symbol)
new_symbols_full.append((symbol, rank, dt))
# Barely any overhead.. lets do it.
with open('symbols_new.txt', 'w+') as f:
f.writelines('\n'.join(new_symbols))
with open('symbols_new.pickle', 'wb+') as f:
pickle.dump(new_symbols_full, f)
time.sleep(2.)
with open('symbols_new.txt', 'w+') as f:
f.writelines('\n'.join(new_symbols))
with open('symbols_new.pickle', 'wb+') as f:
pickle.dump(new_symbols_full, f)
def get_all_second_pass():
symbols = get_all_new_symbols()
new_symbols_full = []
for symbol in symbols:
rank, dt = get(symbol)
if rank != "":
new_symbols_full.append((symbol, rank, dt))
# Barely any overhead.. lets do it.
with open('symbols_new2.pickle', 'wb+') as f:
pickle.dump(new_symbols_full, f)
time.sleep(2.)
with open('symbols_new2.pickle', 'wb+') as f:
pickle.dump(new_symbols_full, f)
def get_all_base_symbols():
with open('nasdaqlisted.txt', 'r') as f:
symbols = f.readlines()
with open('otherlisted.txt', 'r') as f:
symbols.extend(f.readlines())
symbols = [x.strip() for x in symbols]
return symbols
def get_all_new_symbols():
with open('symbols_new.txt', 'r') as f:
symbols = f.readlines()
symbols = [x.strip() for x in symbols]
return symbols
if __name__ == '__main__':
get_all_second_pass()