【问题标题】:Selenium PhantomJS wait for image to be availableSelenium PhantomJS 等待图像可用
【发布时间】:2016-01-24 11:44:45
【问题描述】:

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

问题是,如何确保 selenium 仅在图像的真实 URL 包含在页面中时才抓取图像。我正在考虑检查图像的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>

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

【问题讨论】:

    标签: python selenium web-crawler phantomjs wait


    【解决方案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);
    }
    

    【讨论】:

    • 感谢您的建议,我正在考虑尝试该选项,但对此不太确定。假设有链接的 ajax 请求,在这种情况下,jQuery.active 可能会降为零,即使会有额外的请求进行?此外,它真的可以用于检查GET 图像请求吗?
    • 我相信如果使用 Ajax 进行交互,那么该代码可以帮助您。这实际上取决于您要抓取的网站...关于您的问题,据我所知,如果有活动的 ajax 调用,jQuery.active 不会为零。
    • 嗯,根据我的测试,如果我们为@987654326添加服务员真的没有区别@Selenium默认等待所有ajax完成
    • 你确定吗?您在 Selenium 文档中看到了吗?据我所知,Selenium 具有可配置的默认超时的隐式等待,但我不知道有关 ajax 交互的行为。一个链接会很棒!
    猜你喜欢
    • 2018-01-04
    • 2017-07-05
    • 2021-02-24
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 2013-05-24
    相关资源
    最近更新 更多