How to handle AttributeError: 'NoneType' object has no attribute 'text'?

When parsing periodically get the error "AttributeError: 'NoneType' object has no attribute 'text'", because parser can't find the item you want on the intercom and find out he text.

Data get this:
fax = soup.find(id="ctl0_left_fax")

Then add to the array (the script stumbles): 'fax': fax.text.strip(),

Tried to do a scan:
# Check if page TITLE is blank, it means there is nothing to parse. WANT TO SKIP FURTHER PARSING
if (len(soup.title.text.strip()) == 15) or (soup.title.text.strip() == 'testtesttest -'):
exit
# If the page TITLE is <16 (so there is some content), then see the text selectors
else:
 list = {
 'cap': cap.text.strip(),
 'fax': fax.text.strip(),
 'email': email.text.strip(),
 }


But somehow it is ignored (although checked - works).

I should add that I implemented a similar algorithm in PHP and I have everything working.

Moreover, at the stage of adding to an array I just checked IT via the return data type using the ternary operator, but in Python this is not.
July 2nd 19 at 13:41
1 answer
July 2nd 19 at 13:43
BeautifulSoup is designed such collective way, it gives None if not found element not raise an exception, or the element is empty, so every item that you're looking for, you need to check. Ie:
fax = soup.find(id="ctl0_left_fax")
if fax:
 another_element = fax.find(class_='some_class')
 if another_element:
 another_one = another_element.find(class_='some_another_class')
 if another_one:
 do_something()

Or malaccense decision not to produce branch
fax = soup.find(id="ctl0_left_fax")
another_element = fax and fax.find(class_='some_class')
another_one = another_element and another_element.find(class_='some_another_class')
if another_one:
 do_something()


Here is one tip: don't use BeautifulSoup.

Find more questions by tags PythonBeautiful Soup