Python 3 等同于 HTMLParseError?
Python 3 equivalent of HTMLParseError?
我只是想知道是否有任何 Python 3 等同于 Python 2 中的 HTMLParseError
。HTMLParseError
似乎已被弃用 Python 3.3 向前并在 Python 3.5 中删除。
有什么方法可以在 Python 版本 > 3.5 中捕获 HTMLParseError
吗?
以下是我收到的回溯:
File "/opt/Projects/WAFToast/main.py", line 12, in <module>
HTMLParseError = html.parser.HTMLParseError
AttributeError: module 'html.parser' has no attribute 'HTMLParseError'
谷歌搜索了一下,我found同样的问题正在用下面的补丁修复,恕我直言,这不是一个有益的解决方案:
try:
from html.parser import HTMLParseError
except ImportError: # Python 3.5+
class HTMLParseError(Exception):
pass
如果有人能指出我所缺少的必要内容,那就太好了。 =)
docs 说:
Deprecated since version 3.3, will be removed in version 3.5: This exception has been deprecated because it’s never raised by the parser (when the default non-strict mode is used).
它被删除是因为除非使用(大概)很少使用的严格模式,否则没有任何东西引发它,所以必须问一个问题:"are you using the strict mode?"
如果不是,您可以安全地删除导入和捕获它的代码。
如果是,请检查引发了什么异常(如果有的话),然后导入它。
如果您使用严格模式并且必须支持 Python 的两个版本,您可以按照
的方式做一些事情
try:
from html.parser import HTMLParseError as ParseError
except ImportError: # Python 3.5+
from html.parser import NewTypeOfFancyException as ParseError
然后在适用的地方使用 except ParseError
。
我只是想知道是否有任何 Python 3 等同于 Python 2 中的 HTMLParseError
。HTMLParseError
似乎已被弃用 Python 3.3 向前并在 Python 3.5 中删除。
有什么方法可以在 Python 版本 > 3.5 中捕获 HTMLParseError
吗?
以下是我收到的回溯:
File "/opt/Projects/WAFToast/main.py", line 12, in <module>
HTMLParseError = html.parser.HTMLParseError
AttributeError: module 'html.parser' has no attribute 'HTMLParseError'
谷歌搜索了一下,我found同样的问题正在用下面的补丁修复,恕我直言,这不是一个有益的解决方案:
try:
from html.parser import HTMLParseError
except ImportError: # Python 3.5+
class HTMLParseError(Exception):
pass
如果有人能指出我所缺少的必要内容,那就太好了。 =)
docs 说:
Deprecated since version 3.3, will be removed in version 3.5: This exception has been deprecated because it’s never raised by the parser (when the default non-strict mode is used).
它被删除是因为除非使用(大概)很少使用的严格模式,否则没有任何东西引发它,所以必须问一个问题:"are you using the strict mode?"
如果不是,您可以安全地删除导入和捕获它的代码。
如果是,请检查引发了什么异常(如果有的话),然后导入它。
如果您使用严格模式并且必须支持 Python 的两个版本,您可以按照
的方式做一些事情try:
from html.parser import HTMLParseError as ParseError
except ImportError: # Python 3.5+
from html.parser import NewTypeOfFancyException as ParseError
然后在适用的地方使用 except ParseError
。