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']
15 from lxml.html import fromstring
16 from .bkmk_ph_util import HTMLParser
19 def parse_html(html_text, charset=None, log=None):
23 html_tree = fromstring(html_text)
24 except ValueError as e:
25 if e.args[0].startswith(
26 'Unicode strings with encoding declaration are not supported.'
27 ' Please use bytes input'
31 '<\\?xml version="(\\d|.)+" encoding="([^"]+)"\\?>',
34 charset = match.group(2)
36 html_text = html_text.encode(charset)
37 html_tree = fromstring(html_text)
43 title = html_tree.findtext('head/title')
45 title = html_tree.findtext('title')
47 meta = html_tree.findall('head/meta')
49 if m.get('http-equiv', '').lower() == 'content-type':
50 meta_content = m.get("content")
54 meta_content.lower().split('charset=')[1].split(';')[0]
58 elif m.get('charset', ''):
59 meta_charset = m.get('charset').lower()
64 #if title and (charset or meta_charset):
65 # title = title.encode(charset or meta_charset)
68 if m.get('http-equiv', '').lower() == 'refresh':
69 refresh = m.get("content")
74 for link in html_tree.findall('head/link'):
75 if link.get('rel', '').lower() in ('icon', 'shortcut icon'):
76 icon = link.get("href")
81 if (title is None) and (refresh is None) and (icon is None):
83 return HTMLParser(charset, meta_charset, title, refresh, icon)