股票筛选程序:打印完整列表而不是遍历,条件不正确

2024-09-28 19:25:38 发布

您现在位置:Python中文网/ 问答频道 /正文

这是输出。我只想要“嗯”,下一个是“ABT”。 ['MMM'、'ABT'、'ABV'、'ABMD'、'ACN'、'ATVI'、'ADBE'、'AMD'] 尾随市盈率:17.85 股本回报率:54.34% 收入:32.35亿美元 季度收入增长:-5.00% ['MMM'、'ABT'、'ABV'、'ABMD'、'ACN'、'ATVI'、'ADBE'、'AMD'] 尾随市盈率:55.03 股本回报率:8.28% 收入:3072亿美元 季度收入增长:2.00%

我试着让它打印(库存清单[每个库存]),但它给出了一个错误消息

此外,它总是打印else语句(无股票),即使根据yahoo finance,“MMM”同时满足条件1和条件3,因此它应该打印“MMM”的股票行情和基本面

def scrape(stock_list, interested, technicals):
    condition_1 = float(technicals.get('Return on Equity',0)) > 0
    condition_2 = float(technicals.get('Trailing P/E',0)) > 2
    condition_3 = float(technicals.get('Price/Book (mrq)',0)) <11
    if (condition_1 and condition_3)==True:
        for each_stock in stock_list:
            technicals = scrape_yahoo(each_stock)
            print(stock_list)
            for ind in interested:
                print(ind + ": "+ technicals[ind])
            print("------")
            time.sleep(1) 
    else:
        print('No stocks found')                                                   # Use delay to avoid getting flagged as bot
    return technicals
def main():
    stock_list = ['MMM', 'ABT', 'ABBV', 'ABMD', 'ACN', 'ATVI', 'ADBE', 'AMD']
    interested = ['Trailing P/E', 'Return on Equity', 'Revenue', 'Quarterly Revenue Growth']
    technicals = {}
    tech = scrape(stock_list, interested, technicals)
    print(tech)


main()
def scrape(stock_list, interested, technicals):
    condition_1 = float(technicals.get('Return on Equity',0)) > 0
    condition_2 = float(technicals.get('Trailing P/E',0)) > 2
    condition_3 = float(technicals.get('Price/Book (mrq)',0)) <11
    if (condition_1 and condition_3)==False:
        for each_stock in stock_list:
            technicals = scrape_yahoo(each_stock)
            print(stock_list)
            for ind in interested:
                print(ind + ": "+ technicals[ind])
            print("------")
            time.sleep(1) 
    else:
        print('No stocks found')                                                   # Use delay to avoid getting flagged as bot
    return technicals
def main():
    stock_list = ['MMM', 'ABT', 'ABBV', 'ABMD', 'ACN', 'ATVI', 'ADBE', 'AMD']
    interested = ['Trailing P/E', 'Return on Equity', 'Revenue', 'Quarterly Revenue Growth']
    technicals = {}
    tech = scrape(stock_list, interested, technicals)
    print(tech)


main()```

Tags: getstockfloatconditionlistprintscrapemmm