我如何重新渲染HTML

我如何重新渲染HTML

本文介绍了我如何重新渲染HTML PyQt4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设法使用建议的代码来从网页呈现HTML,然后解析,查找和使用文本。我正在使用PyQt4。但是,我感兴趣的网页经常更新,我想重新渲染页面并检查更新的HTML以获取新信息。



因此,我的pythonscript中有一个循环,这样我就可以重新开始。但是,这会导致程序崩溃。我搜查了网络,发现这是可以预料的,但我没有找到任何关于如何正确执行的建议。 PyQt4.QtGui导入*

从PyQt4.QtCore中导入*

  import * 

from PyQt4.QtWebKit import *


class Render(QWebPage):
$ b $ 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 ()

r =渲染(url)

html = r.frame.toHtml()



当我第二次点击 r = Render(url)时,它崩溃了。 S,o我正在寻找像 r = Rerender(url)之类的东西。

正如你可能猜到的那样,我并不是一个程序员,而我通常通过窃取我几乎不了解的代码来解决问题。但这是我第一次找不到答案,所以我想我应该自己提出一个问题。



我希望我的问题很清楚,有人有答案。

解决方案

简单演示(适应口味):

导入sys,从PyQt4输入
导入QtCore,QtGui,QtWebKit
$ b $ class WebPage(QtWebKit.QWebPage):
def __init __(self ,url):
super(WebPage,self).__ init __()
self.url = url
self.mainFrame()。loadFinished.connect(self.handleLoadFinished)
self .refresh()

def refresh(self):
self.mainFrame()。load(QtCore.QUrl(self.url))

def handleLoadFinished self):
print('Loaded:',self.mainFrame()。url()。toString())
#用html做的东西...
print('Reloading in 3秒... \\\
')
QtCore.QTimer.singleShot(2000,self.refresh)

if __name__ = ='__main__':

signal.signal(signal.SIGINT,signal.SIG_DFL)
app = QtGui.QApplication(sys.argv)
webpage = WebPage('http: ')
print('按Ctrl + C退出'\\ n')
sys.exit(app.exec_())


I have managed to use suggested code in order to render HTML from a webpage and then parse, find and use the text as wanted. I'm using PyQt4. However, the webpage I am interested in is updated frequently and I want to rerender the page and check the updated HTML for new info.

I thus have a loop in my pythonscript so that I sort of start all over again. However, this makes the program crash. I have searched the net and found out that this is to be expected, but I have not found any suggestion on how to do it correctly. It must be simple, I guess?

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()

r = Render(url)

html = r.frame.toHtml()

S,o when I hit r=Render(url) the second time, it crashes. S,o I am looking for something like r = Rerender(url).

As you might guess, I am not much of a programmer, and I usually get by by stealing code I barely understand. But this is the first time I can't find an answer, so I thought I should ask a question myself.

I hope my question is clear enough and that someone has the answer.

解决方案

Simple demo (adapt to taste):

import sys, signal
from PyQt4 import QtCore, QtGui, QtWebKit

class WebPage(QtWebKit.QWebPage):
    def __init__(self, url):
        super(WebPage, self).__init__()
        self.url = url
        self.mainFrame().loadFinished.connect(self.handleLoadFinished)
        self.refresh()

    def refresh(self):
        self.mainFrame().load(QtCore.QUrl(self.url))

    def handleLoadFinished(self):
        print('Loaded:', self.mainFrame().url().toString())
        # do stuff with html ...
        print('Reloading in 3 seconds...\n')
        QtCore.QTimer.singleShot(2000, self.refresh)

if __name__ == '__main__':

    signal.signal(signal.SIGINT, signal.SIG_DFL)
    app = QtGui.QApplication(sys.argv)
    webpage = WebPage('http://en.wikipedia.org/')
    print('Press Ctrl+C to quit\n')
    sys.exit(app.exec_())

这篇关于我如何重新渲染HTML PyQt4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:51