【问题标题】:Accessing an image from a webpage in PyQt4 QtWebkit从 PyQt4 QtWebkit 中的网页访问图像
【发布时间】:2010-05-08 10:05:53
【问题描述】:

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

【问题讨论】:

    标签: python pyqt4 qtwebkit


    【解决方案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 来获取显示的图像。如果您有一个具有 alpha 透明度的 PNG,这会更复杂,但对于不透明的 JPEG,例如它会更容易。 你可以在谷歌上搜索一些网页截图代码来了解如何做到这一点,或者你可以从 Python 中找到的 url 下载。 在 Javascript 中获得 url 变量后,您可能必须使用 this great slideshow 中提供的跨界技术将变量导入 Python 以供下载。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多