Python QWebpage只获取HTML一次,不能再次调用

Python QWebpage只获取HTML一次,不能再次调用,python,pyqt4,qwebpage,Python,Pyqt4,Qwebpage,我有一个密码: from PyQt4 import QtCore from PyQt4.QtWebKit import QWebPage from PyQt4.QtGui import QApplication class TextBrowser(QtCore.QObject): def __init__(self, url): self.some_url = url self.html_source = None QtCore.Q

我有一个密码:

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

class TextBrowser(QtCore.QObject):

    def __init__(self, url):
        self.some_url = url

        self.html_source = None

        QtCore.QObject.__init__(self)
        self.page = QWebPage()

        self.page.loadFinished.connect(self.get_html)

        self.page.mainFrame().load(self.some_url)

    def get_html(self):
        frame = self.page.mainFrame()
        self.html_source = unicode(frame.toHtml()).encode('utf-8')
        QtCore.QCoreApplication.quit()


def get_html_source(some_url):
    app = QApplication([])
    browser = TextBrowser(QtCore.QUrl(some_url))
    app.exec_()
    return browser.html_source
现在,如果我跑步:

print get_html_source('http://www.google.com')
没关系,并从页面返回html源代码http://www.google.com. 但如果我运行下一个这样的程序:

print get_html_source('http://www.google.com')
print get_html_source('http://www.yahoo.com/')
这只执行一次,输出google的html源代码,但在这之后,PyCharm返回过程结束,退出代码139,第二次调用get_html_source不会执行

我需要通过Qwebpage迭代一些url列表并从中获取源代码,但我的实现不起作用

在哪里可以找到有关我的需求或我做错了什么的信息?

考虑以下几点。exec_uu启动事件循环一次,两个单独的页面正在运行:

from PyQt4 import QtCore, QtGui
from PyQt4.QtWebKit import QWebPage
from PyQt4.QtGui import QApplication

class TextBrowser(QtGui.QDialog):

    def __init__(self, url):
        self.some_url = url

        QtCore.QObject.__init__(self)
        self.page = QWebPage()
        self.page.loadFinished.connect(self.get_html)
        self.page.mainFrame().load(self.some_url)

    def get_html(self):
        frame = self.page.mainFrame()
        self.html = frame.toHtml()
        self.close()


def get_html_source():
    app = QApplication([])
    urls = ['http://www.google.com', 'http://www.yahoo.com/']
    out = []
    for u in urls:
        t = TextBrowser(QtCore.QUrl(u))
        t.exec_()
        out.append(t.html)
    print(out)

if __name__ == "__main__":
    get_html_source()

该程序无法按现状退出-我想您希望对HTML做更多的处理,而不是打印它。

您正在创建和执行多个QApplication,因为它们运行着事件循环,因此会相互杀死。您应该只有一个QApplication,并且只调用exec_uuOne一次。问:您真的需要GUI吗?不,这是一个没有GUI的控制台应用程序。在这种情况下,您应该使用QCoreApplication;但是python的urllib有什么问题?urllib.request.urlopen'http://www.google.com“.read python3版本urllib2.urlopen”http://www.google.com“.read python 2 versionQWebpage返回支持javascript的整个html源代码。据我所知,urlib没有。是吗?非常感谢你的回复。但有了这个实现,我就不会遍历URL-s列表了。基本上我需要的东西是:URL=['http://www.google.com', 'http://www.yahoo.com/']对于url中的url:html\u source=get\u html\u sourceurl,但这样它只从列表中的最后一个url返回html\u source。此处存在同步问题-必须等待加载完成。在这种情况下,您需要一个QTimer,它在几秒钟后轮询TextBrowser以获取HTML。我希望这足够让你继续下去。你能建议我在哪里放置那个为你编辑的Qtimer吗。请注意,QApplication在这里没有显式执行,因此可以在退出时获取SEGFULT-这取决于您在退出后要做什么。非常感谢。我不知道我可以使用TextBrowserQtGui.QDialog代替TextBrowserQtCore.QObject。实际上,我不明白对象声明之间的区别。我希望我将来能解决这个问题。现在我在这方面有了一些经验。