0

Can anyone help me with this? I am trying some automated financial analysis. I am following the steps for stock valuation from this article: https://medium.datadriveninvestor.com/use-python-to-value-a-stock-automatically-3b520422ab6

However, when I get to the step for pulling data from finziv, the code gives me the error:

UnboundLocalError: local variable referenced before assignment

Here is the code:

# List of data we want to extract from Finviz Table
metric = ['Price', 'EPS next 5Y', 'Beta', 'Shs Outstand']
def fundamental_metric(soup, metric):
    # the table which stores the data in Finviz has html table attribute class of 'snapshot-td2'
    return soup.find(text = metric).find_next(class_='snapshot-td2').text
   
def get_finviz_data(ticker):
    try:
        url = ("http://finviz.com/quote.ashx?t=" + symbol.lower())
        req = Request(url=url,headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0'}) 
        response = urlopen(req)
        soup = BeautifulSoup(response)
        dict_finviz = {}       
        for m in metric:   
            dict_finviz[m] = fundamental_metric(soup,m)
        for key, value in dict_finviz.items():
            # replace percentages
            if (value[-1]=='%'):
                dict_finviz[key] = value[:-1]
                dict_finviz[key] = float(dict_finviz[key])
            # billion
            if (value[-1]=='B'):
                dict_finviz[key] = value[:-1]
                dict_finviz[key] = float(dict_finviz[key])*1000000000  
            # million
            if (value[-1]=='M'):
                dict_finviz[key] = value[:-1]
                dict_finviz[key] = float(dict_finviz[key])*1000000
            try:
                dict_finviz[key] = float(dict_finviz[key])
            except:
                pass 
    except Exception as e:
        print (e)
        print ('Not successful parsing ' + ticker + ' data.')        
    return dict_finviz

finviz_data = get_finviz_data(ticker)

finviz_data

And here is the error:

name 'symbol' is not defined
Not successful parsing AAPL data.
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
Input In [9], in <cell line: 38>()
     35         print ('Not successful parsing ' + ticker + ' data.')        
     36     return dict_finviz
---> 38 finviz_data = get_finviz_data(ticker)
     40 finviz_data

Input In [9], in get_finviz_data(ticker)
     34     print (e)
     35     print ('Not successful parsing ' + ticker + ' data.')        
---> 36 return dict_finviz
UnboundLocalError: local variable 'dict_finviz' referenced before assignment

Can anyone point me in the right direction or suggest a fix? thanks!

2
  • The parameter to get_finviz_data() is named ticker, but then you're using symbol later on in the function. Aug 11, 2022 at 13:56
  • ive a lot to learn!!! Aug 11, 2022 at 14:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.