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
17 from .bkmk_ph_util import HTMLParser
19 DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic
21 # http://groups.google.com/group/beautifulsoup/browse_thread/thread/69093cb0d3a3cf63
24 class BadDeclParser(BeautifulSoup):
25 def parse_declaration(self, i):
26 """Treat a bogus SGML declaration as raw data. Treat a CDATA
27 declaration as a CData object."""
29 if self.rawdata[i:i+9] == '<![CDATA[':
30 k = self.rawdata.find(']]>', i)
33 data = self.rawdata[i+9:k]
35 self._toStringSubclass(data, CData)
38 j = SGMLParser.parse_declaration(self, i)
39 except SGMLParseError:
40 # Could not parse the DOCTYPE declaration
41 # Try to just skip the actual declaration
43 r'<!DOCTYPE([^>]*?)>', self.rawdata[i:],
44 re.MULTILINE|re.IGNORECASE) # noqa: E227
45 # missing whitespace around bitwise or shift operator
47 toHandle = self.rawdata[i:match.end()]
49 toHandle = self.rawdata[i:]
50 self.handle_data(toHandle)
55 def _parse_html(html_text, charset):
57 return BadDeclParser(html_text, fromEncoding=charset)
62 def parse_html(html_text, charset=None, log=None):
63 root = _parse_html(html_text, charset)
67 _charset = root.originalEncoding
68 if _charset in ("ISO-8859-2", "windows-1252", "MacCyrillic"):
69 # Replace with default and re-parse
70 _charset = DEFAULT_CHARSET
71 root = _parse_html(html_text, _charset)
81 head = html # Some sites put TITLE in HTML without HEAD
84 if (title is None) and (html is not head):
85 # Some sites put TITLE in HTML outside of HEAD
89 # Lookup TITLE in the root
98 if not isinstance(part, basestring):
100 parts.append(part.strip())
101 title = ''.join(parts)
103 meta = head.find(_find_contenttype, recursive=False)
106 meta_content = meta.get("content")
108 __charset = meta_content.lower().split('charset=')[1].\
112 except IndexError: # No charset in the META Content-Type
115 meta_charset = _charset == __charset
120 meta = head.find(_find_charset, recursive=False)
122 meta_content = meta.get("charset")
124 meta_charset = _charset = meta_content.lower()
126 if title and (_charset or meta_charset):
127 title = title.encode(_charset or meta_charset)
129 meta = head.find(_find_refresh, recursive=False)
131 refresh = meta.get("content")
135 meta = head.find(_find_icon, recursive=False)
137 icon = meta.get("href")
141 if (title is None) and (refresh is None) and (icon is None):
143 return HTMLParser(_charset, meta_charset, title, refresh, icon)
146 def _find_contenttype(Tag):
147 return (Tag.name == "meta") and \
148 (Tag.get("http-equiv", '').lower() == "content-type")
151 def _find_charset(Tag):
152 return (Tag.name == "meta") and Tag.get("charset", '')
155 def _find_refresh(Tag):
156 return (Tag.name == "meta") and \
157 (Tag.get("http-equiv", '').lower() == "refresh")
161 return (Tag.name == "link") and \
162 (Tag.get("rel", '').lower() in ('icon', 'shortcut icon'))