Search code examples
pythonbeautifulsoupfinance

Error with stock screener: AttributeError: 'int' object has no attribute 'replace'


I am using the replace function to get rid of the percentage so to can more easily be turned into an integer. In addition, I want to use the replace function in case a N/A appears in the data.

This is for a stock screener I am trying to develop, where it goes through a list of stocks and gives me the remaining ones based off of my criteria.

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' object has no attribute 'replace'


Solution

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

    If technicals does not contain "Return on Equity", you're using integer 0 as the default value, and you can't call replace() on an integer.