X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=parse_html%2Fbkmk_ph_beautifulsoup4.py;h=faafca22eb3ee276b70eddf161c4ae8f30a5327f;hb=8c78e35e801dce4703087810f8fbd1036c68df97;hp=fbd54ff9cfd207c583d5119ba06c20faf23f9d4d;hpb=8a33ee42d7f8edff076734eba8c1718da8127b22;p=bookmarks_db.git diff --git a/parse_html/bkmk_ph_beautifulsoup4.py b/parse_html/bkmk_ph_beautifulsoup4.py index fbd54ff..faafca2 100644 --- a/parse_html/bkmk_ph_beautifulsoup4.py +++ b/parse_html/bkmk_ph_beautifulsoup4.py @@ -11,23 +11,36 @@ __license__ = "GNU GPL" __all__ = ['parse_html'] +import warnings + from bs4 import BeautifulSoup from .bkmk_ph_util import HTMLParser from compat import string_type +warnings.filterwarnings( + 'ignore', 'No parser was explicitly specified') +warnings.filterwarnings( + 'ignore', + "It looks like you're parsing an XML document using an HTML parser.") + universal_charset = "utf-8" DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic def _parse_html(html_text, charset): try: - return BeautifulSoup(html_text, from_encoding=charset) + if isinstance(html_text, bytes): + return BeautifulSoup(html_text, from_encoding=charset) + else: + return BeautifulSoup(html_text) except TypeError: return None def parse_html(html_text, charset=None, log=None): + if not html_text: + return None root = _parse_html(html_text, charset) if root is None: return None @@ -84,12 +97,12 @@ def parse_html(html_text, charset=None, log=None): if meta_content: meta_charset = _charset = meta_content.lower() - if title and (_charset or meta_charset): - try: - title = title.encode(_charset or meta_charset) - except LookupError: - title = title.encode(universal_charset) - _charset = universal_charset + #if title and (_charset or meta_charset): + # try: + # title = title.encode(_charset or meta_charset) + # except LookupError: + # title = title.encode(universal_charset) + # _charset = universal_charset meta = head.find(_find_refresh, recursive=False) if meta: