2016-01-24 63 views
1

我写一个基于通用硒phantomjs蜘蛛访问和抓取网页。 对程序的输入包括需要爬取的模板(css选择器),输出应根据模板生成数据。 如果我们试图从一个网站抓取的图像有时我们可能会得到空的图像(是这样的话,如果网页源代码执行的时间不包括图像),它可以要解决wait发生 然而更具挑战性的问题时,网页为图片提供占位符,后者将通过ajax请求替换为实际图片网址。硒PhantomJS等待图像可用

的问题是,如何确保硒只有一次他们的真实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的更新后的图像?

回答

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); 
} 
+0

感谢的建议,我一直在寻找到想要的是选项,但不真的很确定这一点。假设有链接的ajax请求,那么即使会有额外的请求,“jQuery.active”可能会降为零。此外,它真的可用于检查'GET'图像请求吗? – Yerken

+0

我相信如果交互是通过Ajax完成的,那么该代码可以帮助你。这真的取决于你想要刮去的网站... 关于你的问题,jQuery.active将不会是零,如果有一个积极的ajax调用据我所知。 – narko

+0

普莱斯检查我的编辑:)帮助真的appreaciated – Yerken