if root is None:
return None
- try:
- html = root.html
- except AttributeError:
- if log: log("No HTML in root")
- html = root
-
+ html = root.html
if html is None:
html = root
- try:
- head = html.head
- except AttributeError:
- if log: log("No HEAD in HTML")
- head = html
-
+ head = html.head
if head is None:
head = html # Some sites put TITLE in HTML without HEAD
- try:
- title = head.title.string.encode(_charset)
- except AttributeError:
- title = '' # HEAD but no TITLE
-
- if (not title) and (head is not html):
+ title = head.title
+ if (title is None) and (html is not head):
# Some sites put TITLE in HTML outside of HEAD
+ title = html.title
- try:
- title = html.title.string.encode(_charset)
- except AttributeError:
- title = '' # no TITLE in HTML too
+ if title is None:
+ # Lookup TITLE in the root
+ title = root.title
+
+ if title is None:
+ title = ''
+ elif title.string:
+ title = title.string.encode(_charset)
+ else:
+ title = str(title)
meta = head.find(_find_contenttype, recursive=False)
if meta: