for i in range(1, len(f.find_all('tr'))): AttributeError: 'NoneType' object has no attribute 'find_all'

Viewed 22

Hello guys can someone help me with this , I'm trying to scrape a table it gives me error

Ignoring exception in on_ready Traceback (most recent call last): File "C:\Users\lacha\PycharmProjects\pythonProject1\venv\lib\site-packages\discord\client.py", line 343, in _run_event await coro(*args, **kwargs) File "C:/Users/lacha/Desktop/gagarin/test.py", line 64, in on_ready for i in range(1, len(f.find_all('tr'))): AttributeError: 'NoneType' object has no attribute 'find_all'

f = soup.find('table', {"class": "symbol-table svelte-1q7g1fn"}) 
for i in range(1, len(f.find_all('tr'))): 
    try: 
        a = f.find_all('tr')[i] 
        dat = a.find_all('td')[0] 
        symb = a.find_all('td')[1] 
        name = a.find_all('td')[2] 
        exc = a.find_all('td')[3] 
        price = a.find_all('td')[4] 
        share = a.find_all('td')[5] 
        print(f'{dat.text} {symb.text} {name.text}')
1 Answers

You got the error because if f = soup.find('table',... not find any element for the given class it return None. So you have to add an if statement to check if the value of f is None or not.

f = soup.find('table', {"class": "symbol-table svelte-1q7g1fn"}) 

if f is not None:
    for i in range(1, len(f.find_all('tr'))): 
        try: 
            a = f.find_all('tr')[i] 
            dat = a.find_all('td')[0] 
            symb = a.find_all('td')[1] 
            name = a.find_all('td')[2] 
            exc = a.find_all('td')[3] 
            price = a.find_all('td')[4] 
            share = a.find_all('td')[5] 
            print(f'{dat.text} {symb.text} {name.text}')
        except TypeError:
            pass

else:
    print("Soup not find the element for the given class")
Related