Jython 2.5.1:UnicodeDecodeError

时间:2014-03-03 15:35:45

标签: python beautifulsoup jython-2.5 html5lib qf-test

最近我一直在尝试使用QF-Test 3.5.4中的Jython脚本解析HTML文件中的数据(请注意,根据3.5.1版本的发行说明,受支持的Python版本仅为2.5.1。 - {3 }})。

Python库(旧版因为我需要支持Python 2.x):

  • html5lib 0.95
  • BeautifulSoup 3.2.1

我正在运行Xubuntu 13.10。

Jython脚本如下所示:

    #Script uses obsolete Python libraries because QF-Test only supports Python 2.5.1
import urllib

#BeautifulSoup 3.2.1 - Python 2.x support
import BeautifulSoup

#html5lib 0.95 - has Python 2.5.1 support
from html5lib import sanitizer
from html5lib import treebuilders

#URL of HTML file that has been saved locally
url = 'Tlacovky/$(website)'
fp = urllib.urlopen(url)

#create HTML5 parser
parser = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("beautifulsoup"), tokenizer=sanitizer.HTMLSanitizer)
html5lib_object = parser.parse(file_pointer)
html_string = str(html5lib_object)

#load to BS
soup = BeautifulSoup(html_string)

for content in soup.findAll('script'):
    print content

现在当我尝试使用我需要正确设置的所有变量执行脚本时,我得到了这个:

UnicodeDecodeError: 'unicodeescape' codec can't decode bytes in position 48-54: illegal Unicode character

    at org.python.core.PyException.fillInStackTrace(PyException.java:70)
    at java.lang.Throwable.<init>(Throwable.java:181)
    at java.lang.Exception.<init>(Exception.java:29)
    at java.lang.RuntimeException.<init>(RuntimeException.java:32)
    at org.python.core.PyException.<init>(PyException.java:46)
    at org.python.core.PyException.doRaise(PyException.java:200)
    at org.python.core.Py.makeException(Py.java:1171)
    at org.python.core.Py.makeException(Py.java:1175)
    at org.python.core.Py.makeException(Py.java:1179)
    at org.python.core.Py.makeException(Py.java:1183)
    at qfcommon$py.runscript$52(/opt/qftest/qftest-3.5.4/jython/Lib/qfcommon.py:962)
    at qfcommon$py.call_function(/opt/qftest/qftest-3.5.4/jython/Lib/qfcommon.py)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyBaseCode.call(PyBaseCode.java:182)
    at org.python.core.PyFunction.__call__(PyFunction.java:350)
    at qftest$py.runscript$3(/opt/qftest/qftest-3.5.4/jython/Lib/qftest.py:91)
    at qftest$py.call_function(/opt/qftest/qftest-3.5.4/jython/Lib/qftest.py)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyBaseCode.call(PyBaseCode.java:182)
    at org.python.core.PyFunction.__call__(PyFunction.java:350)
    at org.python.pycode._pyx386.f$0(<string>:1)
    at org.python.pycode._pyx386.call_function(<string>)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyCode.call(PyCode.java:18)
    at org.python.core.Py.runCode(Py.java:1209)
    at org.python.core.Py.exec(Py.java:1253)
    at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:173)
    at de.qfs.apps.qftest.shared.script.JythonEngine.exec(SourceFile:195)
    at org.apache.bsf.BSFManager$6.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.apache.bsf.BSFManager.exec(Unknown Source)
    at de.qfs.apps.qftest.run.RMIRunContext.runScript(SourceFile:1875)
    ... 16 more

我成功地将问题跟踪到输入“inputstream.py”,这是发生错误时的重点。

我真的把这头发拉出来了。如果可以,请帮我解决这个问题。

编辑:

通过修改inputstream.py来修复:

invalid_unicode_re = re.compile("[\u0001-\u0008\u000B\u000E-\u001F\u007F-\u009F\uD800-\uDFFF\uFDD0-\uFDEF\uFFFE\uFFFF\U0001FFFE\U0001FFFF\U0002FFFE\U0002FFFF\U0003FFFE\U0003FFFF\U0004FFFE\U0004FFFF\U0005FFFE\U0005FFFF\U0006FFFE\U0006FFFF\U0007FFFE\U0007FFFF\U0008FFFE\U0008FFFF\U0009FFFE\U0009FFFF\U000AFFFE\U000AFFFF\U000BFFFE\U000BFFFF\U000CFFFE\U000CFFFF\U000DFFFE\U000DFFFF\U000EFFFE\U000EFFFF\U000FFFFE\U000FFFFF\U0010FFFE\U0010FFFF]")

#Craziness
if len("\U0010FFFF") == 1:
            self.reportCharacterErrors = self.characterErrorsUCS4
            self.replaceCharactersRegexp = re.compile("[\uD800-\uDFFF]")
        else:
            self.reportCharacterErrors = self.characterErrorsUCS2
            self.replaceCharactersRegexp = re.compile("([\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF])")

1 个答案:

答案 0 :(得分:1)

[2016年中期大部分改写以更新。]

html5lib不支持Jython 2.5,而且从来没有。在html5lib 0.9999中引入了一定程度的支持,但这需要Jython 2.7(特别是,支持不是保证,但原则上它有效)。

如果您想尝试使用Jython 2.5,您需要的不仅仅是替换invalid_unicode_re,请参阅this bug。我建议尝试使用您的修改来运行测试套件。另请注意,现在我们至少需要Python 2.6,并且对2.5的任何变体的支持现在将需要大量的工作。