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-2012 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]
41 if title and (charset or meta_charset):
42 title = title.encode(charset or meta_charset)
45 if m.get('http-equiv', '').lower() == 'refresh':
46 refresh = m.get("content")
51 for link in html_tree.findall('head/link'):
52 if link.get('rel', '').lower() in ('icon', 'shortcut icon'):
53 icon = link.get("href")
58 if (title is None) and (refresh is None) and (icon is None):
60 return HTMLParser(charset, meta_charset, title, refresh, icon)