Selenium PhantomJS等待图像可用

时间:2016-01-24 11:44:46

标签: python selenium web-crawler phantomjs wait

我正在编写一个基于selenium phantomjs的通用蜘蛛来访问和抓取网页。 程序的输入包括需要爬行的模板(css选择器),输出应根据模板生成数据。 如果我们尝试从网站抓取图像,有时我们可能会得到空图像(如果页面源到执行时不包含图像,那就是这种情况),可以通过wait来解决 然而,当网页为图像提供占位符时会出现更具挑战性的问题,后者后来通过ajax请求用实际图像URL替换。

问题是,如何确保selenium仅在其真实网址包含在网页中时抓取图片。我正在考虑检查图像的src属性是否有变化,只有在单一更改之后我才能开始解析页面源。但是,不确定如何实施?或者,如果这是一个好主意?

修改



<html>

<head>
    <style>
    img {
        width: 100%;
        height: auto;
    }
    </style>
</head>

<body>
    <div id='wrapper'>
        <div class='wrapper-child'>
            <img data-backup='./1clr.jpg' src='./1bw.jpg'>
        </div>
        <div class='wrapper-child'>
            <img data-backup='./2clr.jpg' src='./2bw.jpg'>
        </div>
        <div class='wrapper-child'>
            <img data-backup='./3clr.jpg' src='./3bw.jpg'>
        </div>
    </div>
    <script src='./jquery.js'></script>
    <script type='text/javascript'>
    $(document).ready(function() {
        // setTimeout(function() {
            //replace image placeholders
            $.get("ajax/test.html", function(data) {

            }).always(function() {
                $('img').each(function() {
                    $(this).attr('src', $(this).attr('data-backup'));
                });
            });
        // }, 1000);
    });
    </script>
</body>

</html>
&#13;
&#13;
&#13;

假设我有这个页面,如何在jquery更新后使用selenium来抓取图像?

1 个答案:

答案 0 :(得分:1)

如果网站使用jQuery,您可以检查以下内容,以确保所有ajax交互完成。

jQuery.active == 0

检查此主题是否有相关问题:wait for an ajax call to complete with Selenium 2 web driver

修改

此代码适用于我们:

public static int TIME_OUT_SECONDS = 10;
public static int POLLING_MILLISECONDS = 100;

public static final String JS_JQUERY_DEFINED = "return typeof jQuery != 'undefined';";
public static final String JS_JQUERY_ACTIVE = "return jQuery.active != 0;";
public static final String JS_DOC_READY = "return document.readyState != 'complete';";
public static final String JS_BLOCK = "return typeof $ != 'undefined' &&  typeof $.blockSelenium != 'undefined' && $.blockSelenium==true;";


public static void waitForJQuery(final WebDriver driver) {
    new FluentWait<WebDriver>(driver).withTimeout(TIME_OUT_SECONDS, TimeUnit.SECONDS).pollingEvery(POLLING_MILLISECONDS, TimeUnit.MILLISECONDS).until(new Function<WebDriver, Boolean>() {

        @Override
        public Boolean apply(final WebDriver input) {
            boolean ajax = false;
            boolean jQueryDefined = executeBooleanJavascript(input, JS_JQUERY_DEFINED);


            if (jQueryDefined) {
                ajax |= executeBooleanJavascript(input, JS_JQUERY_ACTIVE);
            }

            boolean ready = executeBooleanJavascript(input, JS_DOC_READY);
            boolean block = executeBooleanJavascript(input, JS_BLOCK);

            ajax |= ready;
            ajax |= block;

            // continue if all ajax request are processed
            return !ajax;
        }
    });

}


private static boolean executeBooleanJavascript(final WebDriver input, final String javascript) {
    return (Boolean) ((JavascriptExecutor) input).executeScript(javascript);
}