2012-11-15 51 views
0

I'just開始學習PyQt4,並使用信號和插槽, 請幫我修復我的代碼,以便在完全下載站點後,它將打印此網頁的html。現在,它只是停止後,「加載完成」在PyQt4的loadFinished信號後打印html網站。 Python

我的代碼:

#! /usr/bin/env python2.7 

from PyQt4.QtCore import * 
from PyQt4.QtGui import * 
from PyQt4.QtWebKit import * 
import sys 

class Browser(QObject): 
    def __init__(self): 
     QObject.__init__(self) 
    def print_percent(self, percent): 
     print percent 
    def print_load_finished(self): 
     print 'Load Finished' 
     self.print_html 
    def print_html(self, ok): 
     frame = QWebView.page().mainFrame() 
     print unicode(frame.toHtml()).encode('utf-8') 

if __name__=='__main__': 
    app = QApplication(sys.argv) 
    view = QWebView() 
    br = Browser() 
    url = QUrl('http://python.org') 
    view.loadFinished.connect(br.print_load_finished) 
    view.loadProgress.connect(br.print_percent) 
    view.load(url) 
    view.show() 
    app.exec_() 

回答

2

您需要print_html方法例如調用

def print_load_finished(self): 
     print 'Load Finished' 
     self.print_html() 

()print_html

結束,但隨後它仍然不會反正作爲你的print_html工作沒有權限查看,你可以通過查看它,但它不是一個正確的面向對象編程的方法。你應該從獲得瀏覽器可能是QWebView並綁定到事件__init__,所以它應該是這樣的

from PyQt4.QtCore import * 
from PyQt4.QtGui import * 
from PyQt4.QtWebKit import * 
import sys 

class Browser(QWebView): 
    def __init__(self): 
     super(Browser, self).__init__() 
     self.loadFinished.connect(self.print_load_finished) 
     self.loadProgress.connect(self.print_percent) 

    def print_percent(self, percent): 
     print percent 

    def print_load_finished(self): 
     print 'Load Finished' 
     self.print_html() 

    def print_html(self): 
     frame = self.page().mainFrame() 
     print unicode(frame.toHtml()).encode('utf-8') 

if __name__=='__main__': 
    app = QApplication(sys.argv) 
    br = Browser() 
    url = QUrl('http://python.org') 
    br.load(url) 
    br.show() 
    app.exec_() 
+0

它的工作原理!非常感謝你! – Vor