From 5240de604c92063107e41cd00c6b363498d65fa6 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Fri, 7 Mar 2008 20:14:12 +0000 Subject: [PATCH] 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 --- Robots/parse_html_beautifulsoup.py | 37 ++++++++++++------------------ 1 file changed, 15 insertions(+), 22 deletions(-) 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: -- 2.39.5