Python 字典与其他条目结合使用时不起作用

Python dictionary not working when combined with additional entries

我正在尝试制作一个 Python 程序来执行以下操作: 给定一个股票代码列表(股票代码),在网站 www.thestreet.com 上形成一个与每个股票代码相关联的链接列表,以找到它们的分析师评级。然后,(这是使用 Mechanize 导入)在网页上搜索评级(买入、持有或卖出)。 Return 带有键(代码)和条目(买入、卖出或持有)的字典。

当我一次输入一个代码时,代码工作正常,Streetdict['AAP'] 例如 returns ['AAP', 'Buy!'] 正如预期的那样。但是,当与其他代码分组时,它会提供我定义的 "Error!"。

代码如下:

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() 之后在不同列表中得到的一些 returns:

[('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!')]

错误应该在上面代码块的最后两个字典 returns 中很明显。

我在 Linux Fedora 24 上使用 Python 2.7.12、Mechanize(最新版本)和 运行 这一切。

如果您需要任何其他信息,请询问。

问题出在 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() 中时,列表被追加两次,一次是 'Buy' 或 'Hold',另一次是 'Error'。要更正该使用 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!")

就是这样,我希望它清楚。