2 HTML Parser using html5.
4 Written by Broytman. Copyright (C) 2010, 2011 PhiloSoft Design
7 from html5lib import HTMLParser as HTML5Parser
8 from .util import HTMLParser
11 def parse_html(filename, charset=None, log=None):
12 parser = HTML5Parser()
14 parser._parse(fp, encoding=charset, parseMeta=bool(charset))
16 html_tree = parser.tree.getDocument()
18 for node in html_tree.childNodes:
19 if (node.name == 'html') and (node.type != 3): # Skip DocType element
28 for node in html.childNodes:
29 if node.name == 'head':
41 for node in head.childNodes:
42 if node.name == 'title':
44 title = node.childNodes[0].value
52 for node in head.childNodes:
53 if node.name == 'meta' and \
54 ('http-equiv' in node.attributes) and \
55 (node.attributes['http-equiv'] == 'content-type'):
56 meta_content = node.attributes['content']
60 meta_content.lower().split('charset=')[1].split(';')[0]
67 charset = parser.tokenizer.stream.charEncoding[0]
69 if charset or meta_charset:
70 title = title.encode(charset or meta_charset)
72 for node in head.childNodes:
73 if node.name == 'meta' and \
74 ('http-equiv' in node.attributes) and \
75 (node.attributes['http-equiv'] == 'refresh'):
76 refresh = node.attributes['content']
79 for node in head.childNodes:
80 if node.name == 'link' and \
81 ('rel' in node.attributes) and \
82 (node.attributes['rel'] in ('icon', 'shortcut icon')):
83 icon = node.attributes['href']
87 for node in html.childNodes:
88 if node.name == 'title':
90 title = node.childNodes[0].value
98 return HTMLParser(charset, meta_charset, title, refresh, icon)