2016-01-24 34 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