Reputation: 109
I am attempting to make a Python program that does the following: Given a list of tickers (stock symbols), form a list of links associated to each one at the website www.thestreet.com, to find their Analyst Rating. Then, (this is using the Mechanize import) search for the rating (buy, hold, or sell) on the webpage. Return a dictionary with key (ticker) and entry (buy, sell, or hold).
The code works fine when I enter one ticker at a time, Streetdict['AAP'] for instance returns ['AAP', 'Buy!'] as expected. However when grouped with other tickers, it provides "Error!" as I have defined it.
Here's the code:
import mechanize
br = mechanize.Browser()
br.set_handle_robots(False) #ignore
br.addheaders = [("User-agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13")]
auto_list = ['AAL','AAP','AN','AZO','BMWYY','BP','CCL','CHH','COP',
'CVX','DAL','ETP','EXPE','F','FCAU','GM','GPI','GRMN','GT','H','HGV',
'HLT','HMC','HOG','IHG','ISCA','JBLU','KMX','LAD','LUV','MAR','MGM',
'NSANY','PBF','PCLN','PII','PK','PSX','RACE','RCL','RYAAY',
'SHLD','SNI','SZKMY','TM','TRIP','TSLA','TTM','UAL',
'VAC','VLKAY','VLVLY','WGO','WYN','XOM'] #auto list from stockpile
#bad: BRK/B, 'RDS/A'
#print(auto_list)
link = 'http://www.thestreet.com/quote/???.html' #"The Street" Analyst
def linker(list): #this creates a list of links from the provided tickers
link_list = []
for i in list:
new_l = link.replace('???', i)
link_list.append(new_l)
return link_list
#defining the ratings
A = '>(Buy)'
C = '>(Hold)'
F = '>(Sell)'
def TheStreetchecker(list): #this checks the buy or sell rating on the website, "The Street" specifically
BorS = []
for i in list:
br.open(i)
if A in br.response().read():
BorS.append("Buy!")
if C in br.response().read():
BorS.append("Hold!")
if F in br.response().read():
BorS.append("Sell!")
else:
BorS.append("Error!")
return BorS
def Streetdict(list):
D1 = zip(list, TheStreetchecker(linker(auto_list)))
print D1
L0 = ['AZO']
Streetdict(L0)
Here are some of the returns I got after running Streetdict() on different lists:
[('AAL', 'Buy!'), ('AAP', 'Error!'), ('AN', 'Buy!'), ('AZO', 'Error!'), ('BMWYY', 'Hold!'), ('BP', 'Error!'), ('CCL', 'Sell!'), ('CHH', 'Error!'), ('COP', 'Hold!'), ('CVX', 'Error!'), ('DAL', 'Buy!'), ('ETP', 'Error!'), ('EXPE', 'Hold!'), ('F', 'Error!'), ('FCAU', 'Hold!'), ('GM', 'Error!'), ('GPI', 'Hold!'), ('GRMN', 'Error!'), ('GT', 'Buy!'), ('H', 'Error!'), ('HGV', 'Hold!'), ('HLT', 'Error!'), ('HMC', 'Buy!'), ('HOG', 'Error!'), ('IHG', 'Hold!'), ('ISCA', 'Error!'), ('JBLU', 'Buy!'), ('KMX', 'Error!'), ('LAD', 'Buy!'), ('LUV', 'Error!'), ('MAR', 'Buy!'), ('MGM', 'Error!'), ('NSANY', 'Buy!'), ('PBF', 'Error!'), ('PCLN', 'Buy!'), ('PII', 'Error!'), ('PK', 'Buy!'), ('PSX', 'Error!'), ('RACE', 'Error!'), ('RCL', 'Buy!'), ('RYAAY', 'Error!'), ('SHLD', 'Buy!'), ('SNI', 'Error!'), ('SZKMY', 'Buy!'), ('TM', 'Error!'), ('TRIP', 'Error!'), ('TSLA', 'Buy!'), ('TTM', 'Error!'), ('UAL', 'Buy!'), ('VAC', 'Error!'), ('VLKAY', 'Buy!'), ('VLVLY', 'Error!'), ('WGO', 'Buy!'), ('WYN', 'Error!'), ('XOM', 'Buy!')]
>>>
================== RESTART: /home/jadamczk/stock_chooser.py ==================
[('AAP', 'Buy!')]
>>>
================== RESTART: /home/jadamczk/stock_chooser.py ==================
[('AAP', 'Buy!'), ('AZO', 'Error!')]
>>>
================== RESTART: /home/jadamczk/stock_chooser.py ==================
[('AZO', 'Buy!')]
The error should be apparent in the last two dictionary returns in the code block above.
I'm using Python 2.7.12, Mechanize (latest version) and running this all on Linux Fedora 24.
If you'd like any other information please ask.
Upvotes: 0
Views: 64
Reputation:
The problem is in the if statements in TheStreetchecker
function here:
for i in list:
br.open(i)
if A in br.response().read(): # When this is true
BorS.append("Buy!") # append with 'Buy' (1st append)
if C in br.response().read(): # then check again if this is true which is not
BorS.append("Hold!")
# here is the problem
if F in br.response().read(): # then check again if this is true which is not
BorS.append("Sell!")
else: # if F is not in br.response().read() which is true
BorS.append("Error!") # append again with Error (2nd append)
With every loop, when A or C are in br.response().read()
the list is appended two times one with 'Buy' or 'Hold' and the other with 'Error'. To correct that use elif
:
for i in list:
br.open(i)
if A in br.response().read():
BorS.append("Buy!")
elif C in br.response().read():
BorS.append("Hold!")
elif F in br.response().read():
BorS.append("Sell!")
else:
BorS.append("Error!")
That's it, I hope it's clear.
Upvotes: 1