2012-10-07 52 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

相关问题