2011-05-24 221 views
1

嘗試使用以下腳本來加載頁面,以便我可以使用JavaScript執行訪問頁面。我想登錄並查看生成的頁面(https://www.thomsononeim.com/v-hom.asp),也可以使用Javascript執行。在Python 2.7,我得到這個錯誤:PyQt4字符編碼:'ascii'編解碼器不能編碼字符

Traceback (most recent call last):
File "C:/Python27/Sample Programs/Stupid Test.py", line 22, in print html UnicodeEncodeError: 'ascii' codec can't encode character u'\xa9' in position 8273: ordinal not in range(128)

下面的代碼:

from __future__ import unicode_literals 
from __future__ import print_function 
from __future__ import division 
import sys 
from PyQt4.QtGui import * 
from PyQt4.QtCore import * 
from PyQt4.QtWebKit import * 


class Render(QWebPage): 
    def __init__(self, url): 
     self.app = QApplication(sys.argv) 
     QWebPage.__init__(self) 
     self.loadFinished.connect(self._loadFinished) 
     self.mainFrame().load(QUrl(url)) 
     self.app.exec_() 

    def _loadFinished(self, result): 
     self.frame = self.mainFrame() 
     self.app.quit() 

url = 'https://www.thomsononeim.com/s-log_in.asp' 
r = Render(url) 
html = r.frame.toHtml() 
print(html) 

回答

4

這應該工作:

print(html.toUtf8()) 
+0

更多的一些信息的鏈接:HTTP:// WWW。 riverbankcomputing.co.uk/static/Docs/PyQt4/html/qstring.html#toUtf8 – 2011-05-24 05:42:05

相關問題