I have a small script that returns the title of a webpage
title = BeautifulSoup(urllib2.urlopen(URL)).title.string
But not all websites specify a <title>
tag, in which case my script returns
AttributeError: 'NoneType' object has no attribute 'string'
Is there a way to have the title
variable equal to the title if there is a title on the webpage?
I am tried
if BeautifulSoup(urllib2.urlopen(url)).title.string.strip():
print BeautifulSoup(urllib2.urlopen(url)).title.string.strip()
else:
print url
But it still raises the AttributeError error
try except
clause?