在PyQt4 QtWebkit中访问网页图片

2 投票
1 回答
1699 浏览
提问于 2025-04-15 22:29

如果一个页面在QWebView上已经完全加载了,我该怎么获取某个图片的数据呢?可能是通过DOM来实现吧?

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,可以先下载 jQuery,然后像这样引入:

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

其实你也可以完全不使用 jQuery,但根据你想做的事情,它通常会让操作变得更简单。

如果你想获取图片的内容作为位图,而不是 url,可能可以使用 HTML 的 canvas 对象,不过我不太确定你会不会遇到跨域安全问题。另一种方法是用 pyQT 来获取图片的显示效果。如果你有一个带透明通道的 PNG 文件,这会更复杂,但如果是一个不透明的 JPEG 文件,比如说,就会简单一些。

你可以在网上搜索一些网页截图的代码,看看怎么做,或者直接用 Python 从找到的 url 下载。

一旦你在 JavaScript 中有了 url 变量,你可能需要使用 这个很棒的幻灯片 中提到的跨域技术,把变量传到 Python 中进行下载。

http://www.sivachandran.in/index.php/blogs/web-automation-using-pyqt4-and-jquery 也可能有有用的示例代码。

撰写回答