python - PyQt4字符编码: 'ascii' codec can't encode character

标签 python character-encoding pyqt4

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

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)

最佳答案

这应该有效:

print(html.toUtf8())

关于python - PyQt4字符编码: 'ascii' codec can't encode character,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6104874/

相关文章:

python - PyQt 4.8 Phenon.VideoPlayer 错误的视频状态

qt - PyQt-QtableView header 。如何添加右上角的小按钮来隐藏/显示带有选中/未选中复选框的列?

python - distribute/distutils 指定 Python 版本

python - 如何聚合热点(探查器)结果并在 kcachegrind 中查看

php - PHP 的 XML 字符编码问题

php - preg_replace() 会改变我的字符集吗?

python - PyQt4 - 拖动后 QGraphicsItem 位置未正确映射到场景中

python - 'Conv2D' 从 1 中减去 3 导致的负尺寸大小

python - 如何在Python中的正则表达式上分割字符串

java - Java Android 应用程序中的文本编码问题