PyQt4 字符编码:'ascii' 编解码器无法编码字符
我想用下面这个脚本来加载一个网页,这样我就可以访问执行了JavaScript的页面。我想登录并查看结果页面(https://www.thomsononeim.com/v-hom.asp),同样是执行了JavaScript的。在Python 2.7中,我遇到了这个错误:
错误追踪(最近的调用在最后):
文件 "C:/Python27/Sample Programs/Stupid Test.py",第22行,在 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 个回答
4
这个应该可以用:
print(html.toUtf8())