pyqt4:循环主渲染类?

时间:2016-01-05 03:53:21

标签: python python-2.7 events pyqt4

我有一个PyQt4类,可以下载我用于报废的网页。

当我将一个url列表传递给Render类,同时实例化它工作正常(单个调用)但是当我尝试使用多个url列表循环[r = Render(url,cb = scrape)]时,之后第一个循环,执行停止或挂起,没有任何错误抛出。

我想分开循环这个类,因为url列表属于不同的类别,并且必须存储单独提取的内容。

我也知道只有一个应用程序可以启动,如果是这样的话如何退出应用程序而不退出它。这样新的网址列表可以被同一个应用程序使用

我一直坚持这个问题。提前致谢

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

class Render(QWebPage):  
  def __init__(self, urls, cb):
    self.app = QApplication(sys.argv)  
    QWebPage.__init__(self)  
    self.loadFinished.connect(self._loadFinished)  
    self.urls = urls  
    self.cb = cb
    self.crawl()  
    self.app.exec_()  

  def crawl(self):  
    if self.urls:  
      url = self.urls.pop(0)  
      print 'Downloading', url  
      self.mainFrame().load(QUrl(url))  
    else:  
      self.app.quit()  

  def _loadFinished(self, result):  
    frame = self.mainFrame()  
    url = str(frame.url().toString())  
    html = frame.toHtml()  
    self.cb(url, html)
    self.crawl()  

def scrape(url, html):
    pass # have scraping code here

url1 = ['http://webscraping.com', 'http://webscraping.com/blog'] 
url2 = ['http://webscraping.com', 'http://webscraping.com/blog']
urls =[]
urls.append(url1)
urls.append(url2)

for url in urls:
    r = Render(url, cb=scrape)

2 个答案:

答案 0 :(得分:1)

问题是你只能实例化一个QApplication对象。这是一个避免这种情况的更新版本,然后在下载URL时只运行Qt的执行循环:

import sys
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebPage

class Render(QWebPage):
  def __init__(self, cb):
    self.app = QApplication(sys.argv)
    QWebPage.__init__(self)
    self.loadFinished.connect(self._loadFinished)
    self.cb = cb

  def crawl(self, url):
    print 'Downloading', url
    self.mainFrame().load(QUrl(url))
    self.app.exec_()

  def _loadFinished(self, result):
    frame = self.mainFrame()
    url = str(frame.url().toString())
    html = frame.toHtml()
    self.cb(url, html)
    self.app.quit()


def scrape(url, html):
  pass # add scraping code here
  print len(html)


r = Render(cb=scrape)
urls = ['http://webscraping.com', 'http://webscraping.com/blog']
for url in urls:
    r.crawl(url)

答案 1 :(得分:0)

不幸的是,@ hoju的回答对我不起作用。

这对我有用(基本上设置一个计时器来检查加载是否完成)。

import sys
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl, QTimer
from PyQt4.QtWebKit import QWebPage


class Render(QWebPage):
    def __init__(self, url):
        QWebPage.__init__(self)
        self.frame = None
        self.mainFrame().loadFinished.connect(self._loadFinished)
        self.mainFrame().load(QUrl(url))

    def _loadFinished(self, result):
        self.frame = self.mainFrame()

def go_again():
    global r, timer, urls
    if(len(urls)>0):
        print("loading",urls[0])
        r = Render(urls.pop())
        timer.start(1000)
    else:
        print("finished")
        sys.exit(app.exec_())

def check_done():
    global r, timer
    if r.frame is not None:
        timer.stop()
        html_result = r.frame.toHtml()
        #do something with html
        print("loaded")
        go_again()

app = QApplication(sys.argv)
urls = ['http://stackoverflow.com/questions/34603886/pyqt4-loop-main-render-class','http://stackoverflow.com/questions/34603886/pyqt4-loop-main-render-class']

timer = QTimer()
timer.timeout.connect(check_done)
#check every second
go_again()
sys.exit(app.exec_())