1 """HTML Parser using lxml.html
3 This file is a part of Bookmarks database and Internet robot.
7 __author__ = "Oleg Broytman <phd@phdru.name>"
8 __copyright__ = "Copyright (C) 2010-2023 PhiloSoft Design"
9 __license__ = "GNU GPL"
11 __all__ = ['parse_html']
14 from lxml.html import fromtring
15 from .bkmk_ph_util import HTMLParser
18 def parse_html(html_text, charset=None, log=None):
19 html_tree = fromtring(html_text)
21 if html_tree.getroot() is None:
24 title = html_tree.findtext('head/title')
26 title = html_tree.findtext('title')
28 meta = html_tree.findall('head/meta')
30 if m.get('http-equiv', '').lower() == 'content-type':
31 meta_content = m.get("content")
35 meta_content.lower().split('charset=')[1].split(';')[0]
39 elif m.get('charset', ''):
40 meta_charset = m.get('charset').lower()
45 #if title and (charset or meta_charset):
46 # title = title.encode(charset or meta_charset)
49 if m.get('http-equiv', '').lower() == 'refresh':
50 refresh = m.get("content")
55 for link in html_tree.findall('head/link'):
56 if link.get('rel', '').lower() in ('icon', 'shortcut icon'):
57 icon = link.get("href")
62 if (title is None) and (refresh is None) and (icon is None):
64 return HTMLParser(charset, meta_charset, title, refresh, icon)