-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathzacks.py
55 lines (45 loc) · 1.77 KB
/
zacks.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
#!/usr/bin/python3
from scrape import scrape_page
BASE_URL = "http://www.zacks.com/stock/quote/"
RATING_XPATH = '//*[@id="premium_research"]/div/table/tbody/tr[1]/td/strong/text()'
PEERS_XPATH = '//*[@id="stock_industry_analysis"]/table/tbody/tr/td[2]/a/span/text()'
def get_rating(ticker_symbol, page=None):
"""
Gets the Zack's Rank Rating of the target ticker symbol
:param ticker_symbol: The ticker symbol of the interested stock (e.g., "AAPL", "GOOG", "MSFT")
:param page: html tree structure based on the html markup of the scraped website
:return: String of Zack's Rank Rating as listed on a stock's Zacks page
"""
if page is None:
page = scrape_page(BASE_URL + ticker_symbol)
rating = page.xpath(RATING_XPATH)
if not rating:
return None
else:
return rating[0]
def get_peers(ticker_symbol, page=None):
"""
Gets the list of Top Peers for a stock as listed on the "Premium Research: Industry Analysis" section
:param ticker_symbol: The ticker symbol of the interested stock (e.g., "AAPL", "GOOG", "MSFT")
:param page: html tree structure based on the html markup of the scraped website
:return: a list of the Top Peers as listed on a stock's "Premium Research: Industry Analysis" section
on it's respective zacks page
"""
if page is None:
page = scrape_page(BASE_URL+ ticker_symbol)
peers = page.xpath(PEERS_XPATH)
if peers:
try:
peers.remove(ticker_symbol.upper())
except:
pass
if peers:
return peers
else:
return None
else:
return None
if __name__ == "__main__":
# Test cases
print (get_rating("AAPL"))
print (get_peers("GOOG"))