Javascript 解释器只在第一页上执行 [英] Javascript interpreter only being executed on the first page

查看:28
本文介绍了Javascript 解释器只在第一页上执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类可以返回任何给定网页的 HTML:

I have the following class to return me the HTML of any given WebPage:

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

from bs4 import BeautifulSoup
from bs4.dammit import UnicodeDammit
import sys
import signal


class Render(QWebPage):
    def __init__(self, url):
        self.app = QApplication(sys.argv)
        QWebPage.__init__(self)
        self.html = None
        signal.signal(signal.SIGINT, signal.SIG_DFL)
        self.connect(self, SIGNAL('loadFinished(bool)'), self._finished_loading)
        self.mainFrame().load(QUrl(url))
        self.app.exec_()

    def _finished_loading(self, result):
        self.html = self.mainFrame().toHtml()
        self.soup = BeautifulSoup(UnicodeDammit(self.html).unicode_markup)
        self.app.quit()   

我有一个循环来遍历需要运行的 JavaScript 网页列表,例如:

And I have a loop to iterate over list of WebPages with JavaScript that need to be run, such as:

l = ["http://host.com/page1", "http://host.com/page2"]

for page in l:
    soup = Render(page).soup
    #Do-something

问题是 JavaScript 代码只在加载的第一个页面中执行,之后不再解释.

Problem is that the JavaScript code is only executed in the first page that's loaded, not interpreting any after that.

推荐答案

可能是页面加载成功了,但是有不止一个框架.更准确地说,有时 page.mainFrame().childFrames() 不为空.您不仅需要处理主框架,还需要处理其子框架.
例如:

It probably the page has been loaded successfully, but it has more than one frame. To be more precise, sometimes page.mainFrame().childFrames() is not empty. You need to process not only the main frame, but also its children.
For example:

def _finished_loading(self, result):
    self.html = self.mainFrame().toHtml()
    self.soup = BeautifulSoup(UnicodeDammit(self.html).unicode_markup)
    # process childFrames
    self.htmls = [frame.toHtml() for frame in self.mainFrame().childFrames()]
    self.soups = [BeautifulSoup(UnicodeDammit(html).unicode_markup) for html in self.htmls]
    self.app.quit()

这篇关于Javascript 解释器只在第一页上执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆