Accessing an image from a web page in PyQt4 QtWebkit

If the page is fully loaded in the QWebView, how can I get the data for a specific image (via the dom perhaps?)

+2


a source to share


1 answer


I'll try to make a hit:

If you want to get url of an image using jQuery you can use this approach:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("http://google.com"))
frame = web.page().mainFrame()

web.show()

def loadFinished(ok):
    print 'loaded'
    frame.evaluateJavaScript("""
    //this is a hack to load an external javascript script 
    //credit to Vincent Robert from http://stackoverflow.com/questions/756382/bookmarklet-wait-until-javascript-is-loaded
    function loadScript(url, callback)
{
        var head = document.getElementsByTagName("head")[0];
        var script = document.createElement("script");
        script.src = url;
        // Attach handlers
        var done = false;
        script.onload = script.onreadystatechange = function()
        {
                if( !done && ( !this.readyState 
                                        || this.readyState == "loaded" 
                                        || this.readyState == "complete") )
                {
                        done = true;
                        // Continue your code
                        callback();
                }
        };

        head.appendChild(script);
}

// This code loads jQuery and executes some code when jQuery is loaded, using above trick
loadScript("http://code.jquery.com/jquery-latest.js", function(){
    //we can inject an image into the page like this:
    $(document.body).append('<img src="http://catsplanet.files.wordpress.com/2009/08/kitten_01.jpg" id="kitten"/>');
    //you can get the url before the image loads like so:
        //detectedKittenImageUrl = $('#kitten').attr('src');
        //alert('detectedKittenImageUrl = ' + detectedKittenImageUrl);
    //but this is how to get the url after it is loaded, by using jquery to bind to it load function:
    $('#kitten').bind('load',function(){
        //the injected image has loaded
        detectedKittenImageUrl = $('#kitten').attr('src');
        alert('detectedKittenImageUrl = ' + detectedKittenImageUrl);
        //Google logo image url is provided by css as opposed to using an IMG tag:
        //it has probabled loaded befor the kitten image which was injected after load
        //we can get the url of Google logo like so:
        detectedGoogleLogoImageUrl = $('#logo').css('background-image');
        alert('detectedGoogleLogoImageUrl = ' + detectedGoogleLogoImageUrl);
    });

});

    """) 

app.connect(web, SIGNAL("loadFinished(bool)"), loadFinished)

sys.exit(app.exec_())

      

If you don't want to download jquery from the internet every time you can download jquery, enter the following:



jQuerySource = open('jquery.min.js').read()
frame.evaluateJavaScript(jQuerySource)

      

you also can't use jQuery at all, but it often makes manipulation easier, depending on what else you want to do.

If you want to get the content of the image as a bitmap and not a URL, perhaps using the html canvas object, maybe I'm not sure if you will run into cross-domain security issues. Another approach would be to use pyQT to get the image as it appears. If you have a PNG with alpha transparency, it would be more difficult, though, but for an opaque JPEG, for example, it would be easier. You could google some webpage to take a screenshot, or you could download it from the found url in Python. After you have your url variable in Javascript, you probably have to use the cross-the-board technique presented in this great slideshow to get the variable in Python to load.

http://www.sivachandran.in/index.php/blogs/web-automation-using-pyqt4-and-jquery might also be a helpful code example.

+1


a source







All Articles