Reputation: 127
How can I catch AttributeError from this line and print message to the user, let's say "you can't change class="post" "
post_start = re.search('<div class="post">', html)
I tried with try, except AttributeError, raise, print but it's always returning nothing. That's what I did:
try:
post_start = re.search('<div class="post">', html)
except AttributeError:
raise
else:
print 'you can't change <div class="post">'
Upvotes: 1
Views: 2965
Reputation: 473803
re.search()
returns None
if no position in the string matches the pattern. Check for None
and raise an exception:
post_start = re.search('<div class="post">', html)
if post_start is None:
raise AttributeError('you can\'t change <div class="post">')
Upvotes: 2