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):
67 root = _parse_html(html_text, charset)
71 _charset = root.originalEncoding
72 if _charset in ("ISO-8859-2", "windows-1252", "MacCyrillic"):
73 # Replace with default and re-parse
74 _charset = DEFAULT_CHARSET
75 root = _parse_html(html_text, _charset)
85 head = html # Some sites put TITLE in HTML without HEAD
88 if (title is None) and (html is not head):
89 # Some sites put TITLE in HTML outside of HEAD
93 # Lookup TITLE in the root
102 if not isinstance(part, string_type):
104 parts.append(part.strip())
105 title = ''.join(parts)
107 meta = head.find(_find_contenttype, recursive=False)
110 meta_content = meta.get("content")
112 __charset = meta_content.lower().split('charset=')[1].\
116 except IndexError: # No charset in the META Content-Type
119 meta_charset = _charset == __charset
124 meta = head.find(_find_charset, recursive=False)
126 meta_content = meta.get("charset")
128 meta_charset = _charset = meta_content.lower()
130 #if title and (_charset or meta_charset):
131 # title = title.encode(_charset or meta_charset)
133 meta = head.find(_find_refresh, recursive=False)
135 refresh = meta.get("content")
139 meta = head.find(_find_icon, recursive=False)
141 icon = meta.get("href")
145 if (title is None) and (refresh is None) and (icon is None):
147 return HTMLParser(_charset, meta_charset, title, refresh, icon)
150 def _find_contenttype(Tag):
151 return (Tag.name == "meta") and \
152 (Tag.get("http-equiv", '').lower() == "content-type")
155 def _find_charset(Tag):
156 return (Tag.name == "meta") and Tag.get("charset", '')
159 def _find_refresh(Tag):
160 return (Tag.name == "meta") and \
161 (Tag.get("http-equiv", '').lower() == "refresh")
165 return (Tag.name == "link") and \
166 (Tag.get("rel", '').lower() in ('icon', 'shortcut icon'))