我明白了
ElementTree.ParseError: reference to invalid character number
当解析包含以下内容作为标记值的 XML 时:locat
我的代码如下:
respXML = httpResponse.content
#also possible respXML = httpResponse.content.decode("utf-8")
#but both get the same error
#this line throws the error
respRoot = ET.fromstring(respXML)
我怎样才能让我的解析器免受看似无效的字符数字的攻击?
最佳答案
这看起来像 html。先看看是否在输入字符串上使用 html 包。 https://pypi.python.org/pypi/html
>>> import html
>>> test = "locat"
>>> html.unescape(test)
'local'
然后将一些已知的 unicode 字符转换为它们的等效字符。即
“ => "
’ => '
...
最后将双空格替换为单空格。
因为预先成功解决所有问题会非常麻烦 - 我建议放置特定的异常(exception)并将错误行写入文件。 通过添加更多规则逐一解决输出文件中的每个错误。
祝你好运。
关于python - 元素树.ParseError : reference to invalid character number,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41067475/