PyQt4 character encoding: 'ascii' codec can't encode character
尝试使用下面的脚本加载一个页面,以便我可以使用执行的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, inprint html UnicodeEncodeError: 'ascii' codec can't encode character
u'\xa9' in position 8273: ordinal not
in range(128)
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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 | print(html.toUtf8()) |