1 """HTML Parser using BeautifulSoup
3 This file is a part of Bookmarks database and Internet robot.
7 __author__ = "Oleg Broytman <phd@phdru.name>"
8 __copyright__ = "Copyright (C) 2007-2023 PhiloSoft Design"
9 __license__ = "GNU GPL"
11 __all__ = ['parse_html']
15 from sgmllib import SGMLParser, SGMLParseError
16 from BeautifulSoup import BeautifulSoup, CData
18 from .bkmk_ph_util import HTMLParser
19 from compat import string_type
21 DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic
23 # http://groups.google.com/group/beautifulsoup/browse_thread/thread/69093cb0d3a3cf63
26 class BadDeclParser(BeautifulSoup):
27 def parse_declaration(self, i):
28 """Treat a bogus SGML declaration as raw data. Treat a CDATA
29 declaration as a CData object."""
31 if self.rawdata[i:i+9] == '<![CDATA[':
32 k = self.rawdata.find(']]>', i)
35 data = self.rawdata[i+9:k]
37 self._toStringSubclass(data, CData)
40 j = SGMLParser.parse_declaration(self, i)
41 except SGMLParseError:
42 # Could not parse the DOCTYPE declaration
43 # Try to just skip the actual declaration
45 r'<!DOCTYPE([^>]*?)>', self.rawdata[i:],
46 re.MULTILINE|re.IGNORECASE) # noqa: E227
47 # missing whitespace around bitwise or shift operator
49 toHandle = self.rawdata[i:match.end()]
51 toHandle = self.rawdata[i:]
52 self.handle_data(toHandle)
57 def _parse_html(html_text, charset):
59 return BadDeclParser(html_text, fromEncoding=charset)
64 def parse_html(html_text, charset=None, log=None):
65 root = _parse_html(html_text, charset)
69 _charset = root.originalEncoding
70 if _charset in ("ISO-8859-2", "windows-1252", "MacCyrillic"):
71 # Replace with default and re-parse
72 _charset = DEFAULT_CHARSET
73 root = _parse_html(html_text, _charset)
83 head = html # Some sites put TITLE in HTML without HEAD
86 if (title is None) and (html is not head):
87 # Some sites put TITLE in HTML outside of HEAD
91 # Lookup TITLE in the root
100 if not isinstance(part, string_type):
102 parts.append(part.strip())
103 title = ''.join(parts)
105 meta = head.find(_find_contenttype, recursive=False)
108 meta_content = meta.get("content")
110 __charset = meta_content.lower().split('charset=')[1].\
114 except IndexError: # No charset in the META Content-Type
117 meta_charset = _charset == __charset
122 meta = head.find(_find_charset, recursive=False)
124 meta_content = meta.get("charset")
126 meta_charset = _charset = meta_content.lower()
128 #if title and (_charset or meta_charset):
129 # title = title.encode(_charset or meta_charset)
131 meta = head.find(_find_refresh, recursive=False)
133 refresh = meta.get("content")
137 meta = head.find(_find_icon, recursive=False)
139 icon = meta.get("href")
143 if (title is None) and (refresh is None) and (icon is None):
145 return HTMLParser(_charset, meta_charset, title, refresh, icon)
148 def _find_contenttype(Tag):
149 return (Tag.name == "meta") and \
150 (Tag.get("http-equiv", '').lower() == "content-type")
153 def _find_charset(Tag):
154 return (Tag.name == "meta") and Tag.get("charset", '')
157 def _find_refresh(Tag):
158 return (Tag.name == "meta") and \
159 (Tag.get("http-equiv", '').lower() == "refresh")
163 return (Tag.name == "link") and \
164 (Tag.get("rel", '').lower() in ('icon', 'shortcut icon'))