最近,我一直試圖使用QF-Test 3.5.4中的Jython腳本解析HTML文件中的數據(請注意,受版本3.5的發行說明支持的Python版本僅爲2.5.1。 1. - http://www.qfs.de/en/qftest/relnotes.html#3.5.1)。Jython 2.5.1:UnicodeDecodeError
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])")
我想我們放棄了這一點,因爲輸出的是一堆亂七八糟的字符。相反,我們現在打算在Java中創建我們自己的解析器並將其與QF-Test集成。 感謝您的回答,現在我們不會再用Jython嘗試這樣的事情了。 – Dan
如果您希望在Java中符合HTML解析器,則有JSoup和Validator.nu HTML解析器。可能是其他人。可能/不一定比寫自己的更好。 – gsnedders