2012-02-27 89 views
2

我的目標是從文檔中獲取所有圖像,然後將大於150x150像素的所有圖像下載到本地。如何從文檔中獲取所有圖像並將其存儲到本地

我被困在從前面的步驟中獲取的URL中檢索文件。這裏是bug的代碼行(全碼 - 在後):

... 
var copyResult = fs.copy(imagesURLs[i], destFile); 
... 

,當我從控制檯運行它只是掛斷上fs.copy(),沒有任何錯誤。 (--load-images = yes,--local-to-remote-url-access =)如果你想設置所有適當的參數,那麼fs.copy()不能用於遠程URL,是)。我是對的還是有一些我做錯了copy()?是否有任何方法直接從webkit的緩存中獲取文件?

得到最新的phantomjs版本和Ubuntu服務器。

我將不勝感激任何形式的幫助。

全部腳本代碼:

if (phantom.args.length < 1 || phantom.args.length > 2) 
{ 
    console.log('Usage: phantomjs ' + phantom.scriptName + ' <URL>'); 
    phantom.exit(); 
} 
else 
{ 
    var page = new WebPage(), 
    address = phantom.args[0]; 

    page.viewportSize = { width: 1200, height: 4000 }; 
    page.open(address, function (status) 
    { 
    if (status === 'success') 
    { 
     var imagesURLs = page.evaluate(function() 
     { 
     var documentImages = [], imagesCount = document.images.length, index = 0; 

     while (index < imagesCount) 
     { 
      if ((document.images[index].width >= 150) && (document.images[index].height >= 150)) 
      { 
      documentImages.push(document.images[index].src); 
      } 

      index++; 
     } 

     return documentImages; 
     }); 

     var fs = require('fs'); 

     for (var i in imagesURLs) 
     { 
     var fileName = imagesURLs[i].replace(/^.*[\\\/]/, ''); 
     var destFile = '' + fs.workingDirectory + '/www/images/' + fileName; 
     console.log(destFile); 

     var copyResult = fs.copy(imagesURLs[i], destFile); 
     console.log(copyResult); 
     } 
    } 
    else 
    { 
     console.log('status: ' + status); 
    } 

    phantom.exit(); 
    }); 
} 

回答

0

人嘗試。

function SaveAs(imgURL) 
{ 
    var oPop = window.open(imgURL,"","width=1, height=1, top=5000, left=5000"); 
    for(;oPop.document.readyState != "complete"; ) 
    { 
    if (oPop.document.readyState == "complete")break; 
    } 
    oPop.document.execCommand("SaveAs"); 
    oPop.close(); 
} 
+0

看起來像這樣「for(; oPop.document.readyState!=」complete「;)」更新結束。 感謝與execCommand的想法,我試圖從中得到一些有用的東西。 – 2012-02-27 10:42:35

相關問題