2012-10-07 122 views
2

今天,在我的代碼即時下載從網站的圖片是這樣的:如何從JavaScript內部下載整個網頁內容,包括圖像?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using HtmlAgilityPack; 
using System.IO; 
using System.Text.RegularExpressions; 
using System.Xml.Linq; 
using System.Net; 
using System.Web; 
using System.Threading; 
using DannyGeneral; 
using GatherLinks; 

namespace GatherLinks 
{ 
    class RetrieveWebContent 
    { 
     HtmlAgilityPack.HtmlDocument doc; 
     string imgg; 
     int images; 

     public RetrieveWebContent() 
     { 
      images = 0; 
     } 

     public List<string> retrieveImages(string address) 
     { 
      try 
      { 
       doc = new HtmlAgilityPack.HtmlDocument(); 
       System.Net.WebClient wc = new System.Net.WebClient(); 
       List<string> imgList = new List<string>(); 
       doc.Load(wc.OpenRead(address)); 
       HtmlNodeCollection imgs = doc.DocumentNode.SelectNodes("//img[@src]"); 
       if (imgs == null) return new List<string>(); 

       foreach (HtmlNode img in imgs) 
       { 
        if (img.Attributes["src"] == null) 
         continue; 
        HtmlAttribute src = img.Attributes["src"]; 

        imgList.Add(src.Value); 
        if (src.Value.StartsWith("http") || src.Value.StartsWith("https") || src.Value.StartsWith("www")) 
        { 
         images++; 
         string[] arr = src.Value.Split('/'); 
         imgg = arr[arr.Length - 1]; 
         wc.DownloadFile(src.Value, @"d:\MyImages\" + imgg); 
        } 
       } 

       return imgList; 
      } 
      catch 
      { 
       Logger.Write("There Was Problem Downloading The Image: " + imgg); 
       return null; 
      } 
     } 
    } 
} 
在許多情況下,圖像背後或在Java腳本和不能被下載定期

但有時。我如何獲得/下載圖像和/或整個完整的網站內容,包括圖像和所有內容,以便在我的硬盤上,我將擁有完整的網站及其所有內容樹,以便我可以離線瀏覽它。

+0

這聽起來很腥......爲了什麼目的? – Guffa

回答

0

我會使用一個真正的瀏覽器,然後保存圖像..看看Watir Webdriver的Ruby解決方案。這個庫可以幫助您自動瀏覽器...我會結合使用它Nokogiri達到你正在嘗試上面做..

Python的等價物也存在..

的webdriver還不支持保存功能,但更舊的「Watir」。你可能也想看看CasperJS,它提供了Javascript語言中的一些瀏覽器自動化。

+0

如果上面的所有內容都不適合你...你可以嘗試Firefox的瀏覽器擴展。 https://addons.mozilla.org/en-US/firefox/addon/save-images/ – sambehera

相關問題