2

ページが QWebView に完全に読み込まれている場合、特定の画像のデータを取得するにはどうすればよいですか (おそらく dom を介して?)

4

1 に答える 1

1

私はこれを突き刺してみます:

jQueryを使用して画像のURLを取得する場合は、次のようなアプローチを使用できます。

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's load function:
    $('#kitten').bind('load',function(){
        //the injected image has loaded
        detectedKittenImageUrl = $('#kitten').attr('src');
        alert('detectedKittenImageUrl = ' + detectedKittenImageUrl);
        //Google's 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's logo like so:
        detectedGoogleLogoImageUrl = $('#logo').css('background-image');
        alert('detectedGoogleLogoImageUrl = ' + detectedGoogleLogoImageUrl);
    });

});

    """) 

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

sys.exit(app.exec_())

jqueryをダウンロードするたびにWebからjqueryをロードしたくない場合は、次のように挿入します。

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

jQueryをまったく使用することもできませんが、他に何をしたいかによっては、操作が簡単になることがよくあります。

画像コンテンツをURLではなくビットマップとして取得したい場合は、htmlキャンバスオブジェクトを使用できる可能性があります。クロスドメインのセキュリティ問題が発生するかどうかはわかりません。別のアプローチは、pyQTを使用して表示された画像を取得することです。アルファ透明度のあるPNGがある場合、これはより複雑になりますが、たとえば不透明なJPEGの場合は簡単です。その方法については、ウェブページのスクリーンショットコードをグーグルで検索するか、Pythonで見つかったURLからダウンロードすることができます。Javascriptでurl変数を取得したら、このすばらしいスライドショーで紹介されているクロスザボーダー手法を使用して、変数をPythonにダウンロードする必要があります。

http://www.sivachandran.in/index.php/blogs/web-automation-using-pyqt4-and-jqueryも便利なサンプルコードです。

于 2010-06-13T23:25:42.520 に答える