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
22 class BadDeclParser(BeautifulSoup):
23 def parse_declaration(self, i):
24 """Treat a bogus SGML declaration as raw data. Treat a CDATA
25 declaration as a CData object."""
27 if self.rawdata[i:i+9] == '<![CDATA[':
28 k = self.rawdata.find(']]>', i)
31 data = self.rawdata[i+9:k]
33 self._toStringSubclass(data, CData)
36 j = SGMLParser.parse_declaration(self, i)
37 except SGMLParseError:
38 # Could not parse the DOCTYPE declaration
39 # Try to just skip the actual declaration
41 r'<!DOCTYPE([^>]*?)>', self.rawdata[i:],
42 re.MULTILINE|re.IGNORECASE) # noqa: E227
43 # missing whitespace around bitwise or shift operator
45 toHandle = self.rawdata[i:match.end()]
47 toHandle = self.rawdata[i:]
48 self.handle_data(toHandle)
53 def _parse_html(html_text, charset):
55 return BadDeclParser(html_text, fromEncoding=charset)
59 def parse_html(html_text, charset=None, log=None):
60 root = _parse_html(html_text, charset)
64 _charset = root.originalEncoding
65 if _charset in ("ISO-8859-2", "windows-1252", "MacCyrillic"): # Replace default
66 _charset = DEFAULT_CHARSET
67 root = _parse_html(html_text, _charset)
77 head = html # Some sites put TITLE in HTML without HEAD
80 if (title is None) and (html is not head):
81 # Some sites put TITLE in HTML outside of HEAD
85 # Lookup TITLE in the root
94 if not isinstance(part, basestring):
96 parts.append(part.strip())
97 title = ''.join(parts)
99 meta = head.find(_find_contenttype, recursive=False)
102 meta_content = meta.get("content")
104 __charset = meta_content.lower().split('charset=')[1].split(';')[0]
107 except IndexError: # No charset in the META Content-Type
110 meta_charset = _charset == __charset
115 meta = head.find(_find_charset, recursive=False)
117 meta_content = meta.get("charset")
119 meta_charset = _charset = meta_content.lower()
121 if title and (_charset or meta_charset):
122 title = title.encode(_charset or meta_charset)
124 meta = head.find(_find_refresh, recursive=False)
126 refresh = meta.get("content")
130 meta = head.find(_find_icon, recursive=False)
132 icon = meta.get("href")
136 if (title is None) and (refresh is None) and (icon is None):
138 return HTMLParser(_charset, meta_charset, title, refresh, icon)
140 def _find_contenttype(Tag):
141 return (Tag.name == "meta") and \
142 (Tag.get("http-equiv", '').lower() == "content-type")
144 def _find_charset(Tag):
145 return (Tag.name == "meta") and Tag.get("charset", '')
147 def _find_refresh(Tag):
148 return (Tag.name == "meta") and \
149 (Tag.get("http-equiv", '').lower() == "refresh")
152 return (Tag.name == "link") and \
153 (Tag.get("rel", '').lower() in ('icon', 'shortcut icon'))