股票筛选器出错:AttributeError:'int'对象没有属性'replace'

时间:2019-06-12 00:22:38

标签: python beautifulsoup finance

我正在使用replace函数摆脱百分比,以便可以更轻松地将其转换为整数。另外,我想使用替换功能,以防数据中出现N / A。

这是我要开发的股票筛选器,它会检查股票列表,并根据我的标准给我剩余的股票。

def scrape(stock_list, interested, technicals):
    condition_1 = float(technicals.get('Return on Equity',0).replace("%","")) > 0
    condition_2 = float(technicals.get('Trailing P/E',0).replace("N/A","")) > 20
    for each_stock in stock_list:
        technicals = scrape_yahoo(each_stock)

        if condition_1 and condition_2:
            print(each_stock)
            for ind in interested:
                print(ind + ": "+ technicals[ind])
            print("------")
            time.sleep(1)                                                    # 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()

AttributeError:'int'对象没有属性'replace'

1 个答案:

答案 0 :(得分:0)

technicals.get('Return on Equity',0).replace("%","")

如果technicals不包含“股本回报率”,则您使用整数0作为默认值,并且不能对整数调用replace()。