本文整理匯總了Python中HTMLParser.HTMLParseError方法的典型用法代碼示例。如果您正苦於以下問題:Python HTMLParser.HTMLParseError方法的具體用法?Python HTMLParser.HTMLParseError怎麽用?Python HTMLParser.HTMLParseError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類HTMLParser
的用法示例。
在下文中一共展示了HTMLParser.HTMLParseError方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: feed
# 需要導入模塊: import HTMLParser [as 別名]
# 或者: from HTMLParser import HTMLParseError [as 別名]
def feed(self, markup):
args, kwargs = self.parser_args
parser = BeautifulSoupHTMLParser(*args, **kwargs)
parser.soup = self.soup
try:
parser.feed(markup)
except HTMLParseError, e:
warnings.warn(RuntimeWarning(
"Python's built-in HTMLParser cannot parse the given document. This is not a bug in Beautiful Soup. The best solution is to install an external parser (lxml or html5lib), and use Beautiful Soup with that parser. See http://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser for help."))
raise e
# Patch 3.2 versions of HTMLParser earlier than 3.2.3 to use some
# 3.2.3 code. This ensures they don't treat markup like <p></p> as a
# string.
#
# XXX This code can be removed once most Python 3 users are on 3.2.3.
示例2: parse_declaration
# 需要導入模塊: import HTMLParser [as 別名]
# 或者: from HTMLParser import HTMLParseError [as 別名]
def parse_declaration(self, i):
"""Treat a bogus SGML declaration as raw data. Treat a CDATA
declaration as a CData object."""
j = None
if self.rawdata[i:i+9] == '<![CDATA[':
k = self.rawdata.find(']]>', i)
if k == -1:
k = len(self.rawdata)
data = self.rawdata[i+9:k]
j = k+3
self._toStringSubclass(data, CData)
else:
try:
j = HTMLParser.parse_declaration(self, i)
except HTMLParseError:
toHandle = self.rawdata[i:]
self.handle_data(toHandle)
j = i + len(toHandle)
return j
示例3: http_response
# 需要導入模塊: import HTMLParser [as 別名]
# 或者: from HTMLParser import HTMLParseError [as 別名]
def http_response(self, request, response):
if not hasattr(response, "seek"):
response = response_seek_wrapper(response)
http_message = response.info()
url = response.geturl()
ct_hdrs = http_message.getheaders("content-type")
if is_html(ct_hdrs, url, self._allow_xhtml):
try:
try:
html_headers = parse_head(response,
self.head_parser_class())
finally:
response.seek(0)
except (HTMLParser.HTMLParseError,
sgmllib.SGMLParseError):
pass
else:
for hdr, val in html_headers:
# add a header
http_message.dict[hdr.lower()] = val
text = hdr + ": " + val
for line in text.split("\n"):
http_message.headers.append(line + "\n")
return response
示例4: feed
# 需要導入模塊: import HTMLParser [as 別名]
# 或者: from HTMLParser import HTMLParseError [as 別名]
def feed(self, markup):
args, kwargs = self.parser_args
parser = BeautifulSoupHTMLParser(*args, **kwargs)
parser.soup = self.soup
try:
parser.feed(markup)
except HTMLParseError, e:
warnings.warn(RuntimeWarning(
"Python's built-in HTMLParser cannot parse the given document. This is not a bug in Beautiful Soup. The best solution is to install an external parser (lxml or html5lib), and use Beautiful Soup with that parser. See http://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser for help."))
raise e
示例5: _parse_error
# 需要導入模塊: import HTMLParser [as 別名]
# 或者: from HTMLParser import HTMLParseError [as 別名]
def _parse_error(self, source):
def parse(source=source):
parser = HTMLParser.HTMLParser()
parser.feed(source)
parser.close()
self.assertRaises(HTMLParser.HTMLParseError, parse)
示例6: StripTags
# 需要導入模塊: import HTMLParser [as 別名]
# 或者: from HTMLParser import HTMLParseError [as 別名]
def StripTags(str):
"""Returns the string with HTML tags stripped.
Args:
str: An html string.
Returns:
The html string with all tags stripped. If there was a parse error, returns
the text successfully parsed so far.
"""
# Brute force approach to stripping as much HTML as possible. If there is a
# parsing error, don't strip text before parse error position, and continue
# trying from there.
final_text = ''
finished = False
while not finished:
try:
strip = _HtmlStripper()
strip.feed(str)
strip.close()
str = strip.get_output()
final_text += str
finished = True
except HTMLParser.HTMLParseError, e:
final_text += str[:e.offset]
str = str[e.offset + 1:]
示例7: feed
# 需要導入模塊: import HTMLParser [as 別名]
# 或者: from HTMLParser import HTMLParseError [as 別名]
def feed(self, data):
try:
HTMLParser.HTMLParser.feed(self, data)
except HTMLParser.HTMLParseError, exc:
raise ParseError(exc)
示例8: feed
# 需要導入模塊: import HTMLParser [as 別名]
# 或者: from HTMLParser import HTMLParseError [as 別名]
def feed(self, data):
try:
HTMLParser.HTMLParser.feed(self, data)
except HTMLParser.HTMLParseError, exc:
raise ParseError(exc)
示例9: strip_tags
# 需要導入模塊: import HTMLParser [as 別名]
# 或者: from HTMLParser import HTMLParseError [as 別名]
def strip_tags(html):
s = HTMLTagStripper()
try:
s.feed(html)
except HTMLParseError:
get_logger().error('error stripping tags', raw_html=html)
return s.get_data()
# https://djangosnippets.org/snippets/19/