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 __all__ = ['parse_html']
16 from lxml.html import parse
17 from .bkmk_ph_util import HTMLParser
20 def parse_html(filename, charset=None, log=None):
21 html_tree = parse(filename)
23 if html_tree.getroot() is None:
26 title = html_tree.findtext('head/title')
28 title = html_tree.findtext('title')
32 meta = html_tree.findall('head/meta')
34 if m.get('http-equiv', '').lower() == 'content-type':
35 meta_content = m.get("content")
39 meta_content.lower().split('charset=')[1].split(';')[0]
46 if charset or meta_charset:
47 title = title.encode(charset or meta_charset)
50 if m.get('http-equiv', '').lower() == 'refresh':
51 refresh = m.get("content")
56 for link in html_tree.findall('head/link'):
57 if link.get('rel', '').lower() in ('icon', 'shortcut icon'):
58 icon = link.get("href")
63 return HTMLParser(charset, meta_charset, title, refresh, icon)