Python handle 'NoneType' object has no attribute 'find_all' error with if else statement
Question: I am using beautifulsoup4 to grab stock data and send to a spreadsheet in
python. The problem I am having is that I cannot get my loop to skip over
attributes that return None. So what I am needing is the code to add null
values to rows where attribute would return none.

    
    
    //my dictionay for storing data
    data = {
        'Fiscal Quarter End' : [],
        'Date Reported' : [],
        'Earnings Per Share' : [],
        'Consensus EPS* Forecast' : [],
        '% Surprise' : []
    }
    
        url = ""
        html = requests.get(url)
        data = html.text
        soup = bs4.BeautifulSoup(data)
    
    
        table = soup.find("div", class_="genTable")
        for row in table.find_all('tr')[1:]:
    
            if row.has_attr('tr'):
    
                cols = row.find_all("td")
                data['Fiscal Quarter End'].append( cols[0].get_text() )
                data['Date Reported'].append( cols[1].get_text() )
                data['Earnings Per Share'].append( cols[2].get_text() )
                data['Consensus EPS* Forecast'].append( cols[3].get_text() )
                data['% Surprise'].append( cols[4].get_text() )
            else:
                //where i need to add in the empty 'n/a' values
                data['Fiscal Quarter End'].append() 
                data['Date Reported'].append()
                data['Earnings Per Share'].append() 
                data['Consensus EPS* Forecast'].append()
                data['% Surprise'].append()
    


Answer: You have used the `data` variable for two different things. The second usage
overwrote your dictionary. It is simpler to just use `html.text` in the call
to `soup.find()`. Try the following:

    
    
    import requests
    import bs4    
    
    # My dictionary for storing data
    data = {
        'Fiscal Quarter End' : [],
        'Date Reported' : [],
        'Earnings Per Share' : [],
        'Consensus EPS* Forecast' : [],
        '% Surprise' : []
        }
    
    empty = 'n/a'
    url = ""
    html = requests.get(url)
    soup = bs4.BeautifulSoup(html.text, "html.parser")
    table = soup.find("div", class_="genTable")
    rows = []
    
    if table:
        rows = table.find_all('tr')[1:]
    
        for row in rows:
            cols = row.find_all("td")
            data['Fiscal Quarter End'].append(cols[0].get_text())
            data['Date Reported'].append(cols[1].get_text())
            data['Earnings Per Share'].append(cols[2].get_text())
            data['Consensus EPS* Forecast'].append(cols[3].get_text())
            data['% Surprise'].append(cols[4].get_text())
    
    if len(rows) == 0:
        # Add in the empty 'n/a' values if no columns found
        data['Fiscal Quarter End'].append(empty) 
        data['Date Reported'].append(empty)
        data['Earnings Per Share'].append(empty) 
        data['Consensus EPS* Forecast'].append(empty)
        data['% Surprise'].append(empty)    
    

In the event the `table` or `rows` was empty, `data` would hold the following:

    
    
    {'Date Reported': ['n/a'], 'Earnings Per Share': ['n/a'], '% Surprise': ['n/a'], 'Consensus EPS* Forecast': ['n/a'], 'Fiscal Quarter End': ['n/a']}
    

