1 """HTML Parser using lxml.html
3 This file is a part of Bookmarks database and Internet robot.
6 __version__ = "$Revision$"[11:-2]
7 __revision__ = "$Id$"[5:-2]
8 __date__ = "$Date$"[7:-2]
9 __author__ = "Oleg Broytman <phd@phdru.name>"
10 __copyright__ = "Copyright (C) 2010, 2011 PhiloSoft Design"
11 __license__ = "GNU GPL"
13 from lxml.html import parse
14 from .util import HTMLParser
17 def parse_html(filename, charset=None, log=None):
18 html_tree = parse(filename)
20 if html_tree.getroot() is None:
23 title = html_tree.findtext('head/title')
25 title = html_tree.findtext('title')
29 meta = html_tree.findall('head/meta')
31 if m.get('http-equiv', '').lower() == 'content-type':
32 meta_content = m.get("content")
36 meta_content.lower().split('charset=')[1].split(';')[0]
43 if charset or meta_charset:
44 title = title.encode(charset or meta_charset)
47 if m.get('http-equiv', '').lower() == 'refresh':
48 refresh = m.get("content")
53 for link in html_tree.findall('head/link'):
54 if link.get('rel', '').lower() in ('icon', 'shortcut icon'):
55 icon = link.get("href")
60 return HTMLParser(charset, meta_charset, title, refresh, icon)