0

I'm using the below boilerplate PyQt4 code with the goal of capturing all the HTML generated by javascript on a page:

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

def getHtml(str_url):
    r_html = Render(str_url)  
    html = r_html.frame.toHtml()

    return html

I then created a test page to see if it works:

<html>

<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

    <script type="text/javascript">

    $(document).ready(function() {

        $('#test').text('This is a test!')
    });
    </script>
</head>

<body>

    <div id="test"></div>

</body>

</html>

so running

getHtml('http://www.mytestpage.com')

I'd expect to see the HTML with the 'This is a test!' text rendered in the div. However the HTML is being returned with that piece absent.

What am I doing wrong? Is the code not waiting for page to fully load? Or am I misunderstanding the use case?

Ja8zyjits
  • 1,433
  • 16
  • 31
ChrisArmstrong
  • 2,491
  • 8
  • 37
  • 60

2 Answers2

0

Your forget http: in <script src = ...> Link. (= =" And why your don't check it first) It should be;

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

If your want to put web on PyQt4, I recommend using QtWebKit.QWebView to show webpage.

Little example;

import sys
from PyQt4 import QtGui, QtCore, QtWebKit

class QCustomWebView (QtWebKit.QWebView):
    def __init__ (self, parent = None):
        super(QCustomWebView, self).__init__(parent)
        # self.load(QtCore.QUrl('https://www.google.jp')) # Test web link
        self.load(QtCore.QUrl('hello.html')) # Test html file
        self.settings().setAttribute(QtWebKit.QWebSettings.JavascriptEnabled, True)

myQApplication = QtGui.QApplication([])
myQCustomWebView = QCustomWebView()
myQCustomWebView.show()
sys.exit(myQApplication.exec_())

Note : hello.html is your html file.

Bandhit Suksiri
  • 3,390
  • 1
  • 18
  • 20
  • the double slash is merely a shortcut for http://. See http://stackoverflow.com/questions/9646407/two-forward-slashes-in-a-url-src-href-attribute for more details. – ChrisArmstrong Aug 25 '14 at 11:08
0

The issue was that I did not have PyQt4 properly installed on my system. Reinstalling fixed the issue. Sigh

ChrisArmstrong
  • 2,491
  • 8
  • 37
  • 60
  • Hummm... I have PyQt4 on my system. But I can't run your html code. – Bandhit Suksiri Aug 24 '14 at 15:46
  • Why not? Do you get an error? If you are just copying to an html and trying to load it locally, it will not work because jquery won't load properly. Needs to be loaded via webserver to work. – ChrisArmstrong Aug 25 '14 at 11:03
  • I mean your html file not work. Your forget 'http:' as my answer says. And I run by file not link address. If I add 'http:' in link jquery, It's run work; else It can't run. (But, I don't know why I can't use '\\' shorthand) – Bandhit Suksiri Aug 25 '14 at 11:41