X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=parse_html%2Fbkmk_ph_beautifulsoup4.py;h=7687c755e94aa6c80a6d019239efd170554cc4b6;hb=2b3829aef193cb1951989a8cf97a96dcbfc084a1;hp=1095ebce8d510c22df2c9768baed3c27777e1909;hpb=9e2bf3eaee5a8ee84c61ee5eac9c55090d45f63f;p=bookmarks_db.git diff --git a/parse_html/bkmk_ph_beautifulsoup4.py b/parse_html/bkmk_ph_beautifulsoup4.py index 1095ebc..7687c75 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 @@ -56,9 +69,12 @@ def parse_html(html_text, charset=None, log=None): else: parts = [] for part in title: - if not isinstance(part, string_type): - part = part.decode() - parts.append(part.strip()) + #if not isinstance(part, string_type): + # part = part.decode() + if part.strip: + parts.append(part.strip()) + else: + parts.append(' ') # Skip tags, they're usually `
` title = ''.join(parts) meta = head.find(_find_contenttype, recursive=False)