1 """HTML Parser using lxml.html
3 This file is a part of Bookmarks database and Internet robot.
6 __author__ = "Oleg Broytman <phd@phdru.name>"
7 __copyright__ = "Copyright (C) 2010-2013 PhiloSoft Design"
8 __license__ = "GNU GPL"
10 __all__ = ['parse_html']
13 from lxml.html import parse
14 from .bkmk_ph_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')
27 meta = html_tree.findall('head/meta')
29 if m.get('http-equiv', '').lower() == 'content-type':
30 meta_content = m.get("content")
34 meta_content.lower().split('charset=')[1].split(';')[0]
38 elif m.get('charset', ''):
39 meta_charset = m.get('charset').lower()
44 if title and (charset or meta_charset):
45 title = title.encode(charset or meta_charset)
48 if m.get('http-equiv', '').lower() == 'refresh':
49 refresh = m.get("content")
54 for link in html_tree.findall('head/link'):
55 if link.get('rel', '').lower() in ('icon', 'shortcut icon'):
56 icon = link.get("href")
61 if (title is None) and (refresh is None) and (icon is None):
63 return HTMLParser(charset, meta_charset, title, refresh, icon)