X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=parse_html%2Fbkmk_ph_html5.py;h=6d0d38094bea6bd8935b4ddb29455ca458cd7cbf;hb=a04eaa0346e8aa5ad86a195f8f4d36487ebfe09c;hp=a4906288af13fe5da0cf3f24f39e30a010f7cbe7;hpb=80054d3bdcbde67d3b941e0b69b55ca4998f4673;p=bookmarks_db.git diff --git a/parse_html/bkmk_ph_html5.py b/parse_html/bkmk_ph_html5.py index a490628..6d0d380 100644 --- a/parse_html/bkmk_ph_html5.py +++ b/parse_html/bkmk_ph_html5.py @@ -1,10 +1,11 @@ """HTML Parser using html5 This file is a part of Bookmarks database and Internet robot. + """ __author__ = "Oleg Broytman " -__copyright__ = "Copyright (C) 2010-2012 PhiloSoft Design" +__copyright__ = "Copyright (C) 2010-2014 PhiloSoft Design" __license__ = "GNU GPL" __all__ = ['parse_html'] @@ -14,19 +15,17 @@ from html5lib import HTMLParser as HTML5Parser from .bkmk_ph_util import HTMLParser -def parse_html(filename, charset=None, log=None): +def parse_html(html_text, charset=None, log=None): parser = HTML5Parser() - fp = open(filename) - parser._parse(fp, encoding=charset, parseMeta=bool(charset)) - fp.close() - html_tree = parser.tree.getDocument() - - for node in html_tree.childNodes: - if (node.name == 'html') and (node.type != 3): # Skip DocType element - html = node - break - else: - html = None + html_tree = parser.parse(html_text, encoding=charset, parseMeta=bool(charset)) + + html = None + if hasattr(html_tree, 'childNodes'): + for node in html_tree.childNodes: + # Skip DocType element + if (node.name == 'html') and (node.type != 3): + html = node + break if not html: return None @@ -53,7 +52,7 @@ def parse_html(filename, charset=None, log=None): title = '' for node in head.childNodes: - if node.name == 'meta' and \ + if (node.name == 'meta') and \ ('http-equiv' in node.attributes) and \ (node.attributes['http-equiv'] == 'content-type'): meta_content = node.attributes['content'] @@ -65,6 +64,9 @@ def parse_html(filename, charset=None, log=None): meta_charset = False else: break + elif (node.name == 'meta') and ('charset' in node.attributes): + meta_charset = node.attributes['charset'].lower() + break if not charset: charset = parser.tokenizer.stream.charEncoding[0]