从 PyQt4 QtWebkit 中的网页访问图像

发布于 2024-08-31 23:15:06 字数 52 浏览 11 评论 0原文

如果页面已完全加载到 QWebView 上,我如何获取特定图像的数据(可能通过 dom?)

If a page has fully loaded on a QWebView, how can I get the data for a certain image (probably through the dom?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

涫野音 2024-09-07 23:15:06

我会尝试尝试一下:

如果您想使用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画布对象,我不确定你是否会遇到跨域安全问题。
另一种方法是使用 pyQT 获取显示的图像。如果您有一个具有 alpha 透明度的 PNG,这会更复杂,但对于不透明的 JPEG,这会更容易。
您可以通过 Google 搜索一些网页屏幕截图代码来了解如何执行此操作,或者您可以从找到的 Python 网址下载。
一旦你在 Javascript 中有了 url 变量,你可能必须使用 这张很棒的幻灯片将变量放入Python中以供下载。

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

I'll try taking a stab at this:

If you want to get the url of an image using jQuery you could use an approach like this:

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_())

If you didn't want to load jquery from the web each time you could download jquery then inject like this:

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

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

If you want to get the image content as a bitmap not a url, it MAY be possible using a html canvas object, I'm not sure if you will run into cross-domain security problems.
Another approach would be to use pyQT to get the image as it appears. If you have a PNG with alpha-transparency, this would be more complex though, but for an opaque JPEG, for example it would be easier.
You could Google around for some webpage screenshot code for how to do that or you could download from the found url in Python.
Once you had the url variable in Javascript, you would probably have to use the cross-the-boarder technique featured on this great slideshow to get the variable into Python for downloading.

http://www.sivachandran.in/index.php/blogs/web-automation-using-pyqt4-and-jquery may be useful example code too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文