我正在尝试制作一个执行以下操作的Python程序: 给出一个代码清单(股票代码),在网站www.thestreet.com上形成一个与每个代码相关联的链接列表,以找到他们的分析师评级。然后,(这是使用Mechanize导入)在网页上搜索评级(购买,保留或出售)。返回带有密钥(股票代码)和条目(买入,卖出或持有)的字典。
当我一次输入一个自动收报机时,代码工作正常,例如Streetdict [' AAP']返回[' AAP','买!'正如所料。但是,当与其他代码组合在一起时,它会提供"错误!"正如我所定义的那样。
以下是代码:
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)
以下是我在不同名单上运行Streetdict()后得到的一些回报:
[('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!')]
错误应该在上面代码块的最后两个字典返回中显而易见。
我正在使用Python 2.7.12,Mechanize(最新版本)并在Linux Fedora 24上运行这一切。
如果您有任何其他信息,请询问。
答案 0 :(得分:1)
问题在于TheStreetchecker
函数中的if语句:
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)
对于每个循环,当A或C在br.response().read()
中时,列表将附加两次,其中“买入”或“保留”,另一次带有“错误”。要更正使用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!")
就是这样,我希望它很清楚。