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"): # Replace default
69 _charset = DEFAULT_CHARSET
70 root = _parse_html(html_text, _charset)
80 head = html # Some sites put TITLE in HTML without HEAD
83 if (title is None) and (html is not head):
84 # Some sites put TITLE in HTML outside of HEAD
88 # Lookup TITLE in the root
97 if not isinstance(part, basestring):
99 parts.append(part.strip())
100 title = ''.join(parts)
102 meta = head.find(_find_contenttype, recursive=False)
105 meta_content = meta.get("content")
107 __charset = meta_content.lower().split('charset=')[1].split(';')[0]
110 except IndexError: # No charset in the META Content-Type
113 meta_charset = _charset == __charset
118 meta = head.find(_find_charset, recursive=False)
120 meta_content = meta.get("charset")
122 meta_charset = _charset = meta_content.lower()
124 if title and (_charset or meta_charset):
125 title = title.encode(_charset or meta_charset)
127 meta = head.find(_find_refresh, recursive=False)
129 refresh = meta.get("content")
133 meta = head.find(_find_icon, recursive=False)
135 icon = meta.get("href")
139 if (title is None) and (refresh is None) and (icon is None):
141 return HTMLParser(_charset, meta_charset, title, refresh, icon)
144 def _find_contenttype(Tag):
145 return (Tag.name == "meta") and \
146 (Tag.get("http-equiv", '').lower() == "content-type")
149 def _find_charset(Tag):
150 return (Tag.name == "meta") and Tag.get("charset", '')
153 def _find_refresh(Tag):
154 return (Tag.name == "meta") and \
155 (Tag.get("http-equiv", '').lower() == "refresh")
159 return (Tag.name == "link") and \
160 (Tag.get("rel", '').lower() in ('icon', 'shortcut icon'))