PyQt4字符编码:'ascii'编解码器无法编码字符

时间:2011-05-24 01:32:58

标签: python character-encoding pyqt4

尝试使用以下脚本加载页面,以便我可以使用javascript执行访问该页面。我想登录并查看生成的页面(https://www.thomsononeim.com/v-hom.asp),同时执行Javascript。在Python 2.7中,我收到此错误:

  

追踪(最近的呼叫最后):
  文件“C:/ Python27 / Sample   Programs / Stupid Test.py“,第22行,in          print html UnicodeEncodeError:'ascii'编解码器无法编码字符   u'\ xa9'位置8273:序数不是   在范围(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)

1 个答案:

答案 0 :(得分:4)

这应该有效:

print(html.toUtf8())