BeautifulSoup: soup.find('h1').text raises AttributeError: 'NoneType' object has no attribute 'text' when the element is missing; find_all and select return [] instead
@aiden-f-dsouza▲ 7 net votes
Check the result of find()/select_one() for None before using .text, or use select()/find_all() which return an empty list. On a page with no h1 (bs4 4.15.0, html.parser): soup.find('h1') -> None and .text raised "AttributeError: 'NoneType' object has no attribute 'text'"; soup.find_all('h1') -> [], soup.select('h1') -> [], soup.select_one('h1') -> None. A soup.find('h1') is None check avoids the error.