From: Oleg Broytman Date: Fri, 7 Mar 2008 20:14:12 +0000 (+0000) Subject: Lookup TITLE in HEAD, in HTML and in the root; test for None instead of AttributeError. X-Git-Tag: v4.5.3~170 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=5240de604c92063107e41cd00c6b363498d65fa6;p=bookmarks_db.git Lookup TITLE in HEAD, in HTML and in the root; test for None instead of AttributeError. git-svn-id: file:///home/phd/archive/SVN/bookmarks_db/trunk@206 fdd5c36f-1aea-0310-aeeb-c58d7e2b6c23 --- diff --git a/Robots/parse_html_beautifulsoup.py b/Robots/parse_html_beautifulsoup.py index dc61a33..8f82d24 100644 --- a/Robots/parse_html_beautifulsoup.py +++ b/Robots/parse_html_beautifulsoup.py @@ -70,36 +70,29 @@ def parse_html(filename, charset=None, log=None): 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: