My problem is a general one, how to chain a series of attribute lookups when one of the intermediate ones might return None
, but since I ran into this problem t
The most straightforward way is to wrap in a try
...except
block.
try:
title = soup.head.title.string
except AttributeError:
print "Title doesn't exist!"
There's really no reason to test at each level when removing each test would raise the same exception in the failure case. I would consider this idiomatic in Python.