2013-10-14 167 views
0

例如:http://www.test.com 我的程序正在挖掘抓取。 所以我希望它會每次下載所有的文件。我有一個網站的鏈接如何從網站下載所有文件?

例如:

using (WebClient Client = new WebClient()) 
{ 
    Client.DownloadFile("http://www.abc.com/file/song/a.mpeg", "a.mpeg"); 
} 

這將只下載特定文件a.mpeg。 我想要做的事,如:

using (WebClient Client = new WebClient()) 
{ 
    Client.DownloadFile(address, "*.*"); 
} 

由於地址改變所有的時間,我想下載的所有文件不是一個特定的文件如MPEG或JPG或AVI ......任何extetion。

做「」是正確的方法嗎?

編輯**

這是如何我下載圖像的今天:

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> retrieveFiles(string address) 
     { 

     } 

     public List<string> retrieveImages(string address) 
     { 

      System.Net.WebClient wc = new System.Net.WebClient(); 
      List<string> imgList = new List<string>(); 
      try 
      { 
        doc = new HtmlAgilityPack.HtmlDocument(); 
        doc.Load(wc.OpenRead(address)); 
        string t = doc.DocumentNode.InnerText; 
        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]; 
          //imgg = Path.GetFileName(new Uri(src.Value).LocalPath); 
          //wc.DownloadFile(src.Value, @"d:\MyImages\" + imgg); 
          wc.DownloadFile(src.Value, "d:\\MyImages\\" + Guid.NewGuid() + ".jpg"); 
         } 
        } 
       return imgList; 
      } 
      catch 
      { 
       Logger.Write("There Was Problem Downloading The Image: " + imgg); 
       return null; 

      } 
     } 
    } 
} 

現在在這個地方的代碼:

public List<string> retrieveFiles(string address) 
     { 

     } 

我不想只下載JPG文件但任何類型的文件。 如果鏈接是例如:http://tes.com \ i.jpg爲什麼我需要解析網站,而不是保存,因爲它以某種方式?

回答

3

不,WebClient.DownloadFile永遠不會像Crawler一樣行事。您需要下載該頁面並在返回的HTML頁面上使用C# HtmlParser,列舉您感興趣的資源並單獨下載它們。

+0

不,我已經爬行了。每次在我的程序中變量地址都在變化。我需要的是像*。*之類的過濾器,所以它會下載地址的文件。無論什麼延伸。爬行我已經擁有它了。這個WebClient已經連接到爬行。我只需要找到如何確保它可以下載任何類型的文件,而不是像mpeg jpg或avi這樣的特定文件。 –

+1

不確定您的抓取工具的解釋與我的相同。 –

+0

我不需要webcrawler,我不需要下載一批文件togeather。我只需要使它知道下載任何類型的文件,而不僅僅是特定的文件。如果地址變量包含:http://www.test.com \ i.jpg,所以它會自動下載i.jpg,如果地址與i.bmp結尾,那麼它將知道要下載i.bmp –