Apr-22-2021, 11:45 AM
It's common to get some error when try to scraping a lot of pages.
There can be different way handle it,can make an
There can be different way handle it,can make an
AttributeError
error as a demo.from bs4 import BeautifulSoup html = '''\ <tr> <td id="BMW">Black color</td> <td>2014 model</td> </tr>''' soup = BeautifulSoup(html, 'lxml') for td in soup.find('td', id="BMW").find_next('td'): print(td)
Output:2014 model
So if change id to Lada🚗 get an AttributeError(some would say it's a big error😲),then can catch it with try: except
.from bs4 import BeautifulSoup html = '''\ <tr> <td id="BMW">Black color</td> <td>2014 model</td> </tr>''' soup = BeautifulSoup(html, 'lxml') try: for td in soup.find('td', id="Lada").find_next('td'): print(td) except AttributeError: print('Got an error AttributeError') td = 'Dummy value' print(td)
Output:Got an error AttributeError
Dummy value
Do nothing just skip error would be.except AttributeError: pass