2012-05-15 4 views
-1

이 두 가지 기능이 있습니다. 이제 웹 사이트의 특정 이미지를 url 변수에 다운로드하여 저장합니다.사이트에서 .cs 파일을 다운로드하려면 어떻게해야합니까?

하지만 이제는 GetAllImages() 함수 내용을 변경하고 csFiles 문자열 웹 사이트를 사용하여 사이트에서 모든 .cs 파일을 가져 와서 하드 디스크에 저장하려고합니다. 그래서 GetAllImages 함수를 변경하고 DownloadImage 함수를 수정하여 이미지가 아닌 * .cs 파일을 다운로드해야합니다.

어떻게 할 수 있습니까?

public void GetAllImages() 
     { 

      // Bing Image Result for Cat, First Page 
      string url = "http://www.bing.com/images/search?q=cat&go=&form=QB&qs=n"; 
      string csFiles = "http://open-hardware-monitor.googlecode.com/svn/trunk/Hardware/"; 

      // For speed of dev, I use a WebClient 
      WebClient client = new WebClient(); 
      string html = client.DownloadString(url); 

      // Load the Html into the agility pack 
      HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 
      doc.LoadHtml(html); 

      // Now, using LINQ to get all Images 
      /*List<HtmlNode> imageNodes = null; 
      imageNodes = (from HtmlNode node in doc.DocumentNode.SelectNodes("//img") 
          where node.Name == "img" 
          && node.Attributes["class"] != null 
          && node.Attributes["class"].Value.StartsWith("sg_t") 
          select node).ToList();*/ 

      var imageLinks = doc.DocumentNode.Descendants("img") 
    .Where(n => n.Attributes["class"].Value == "sg_t") 
    .Select(n => HttpUtility.ParseQueryString(n.Attributes["src"].Value)["amp;url"]).ToList(); 


      foreach (string node in imageLinks) 
      { 
       y++; 
       //Console.WriteLine(node.Attributes["src"].Value); 
       richTextBox1.Text += node + Environment.NewLine; 
       Image t = DownloadImage(node); 
       t.Save(@"d:\test\" + y.ToString("D6" + ".jpg")); 

      } 


     } 


     public Image DownloadImage(string _URL) 
     { 
      Image _tmpImage = null; 

      try 
      { 
       // Open a connection 
       System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(_URL); 

       _HttpWebRequest.AllowWriteStreamBuffering = true; 

       // You can also specify additional header values like the user agent or the referer: (Optional) 
       //_HttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"; 
       //_HttpWebRequest.Referer = "http://www.google.com/"; 

       // set timeout for 20 seconds (Optional) 
       _HttpWebRequest.Timeout = 60000; 

       // Request response: 
       System.Net.WebResponse _WebResponse = _HttpWebRequest.GetResponse(); 

       // Open data stream: 
       System.IO.Stream _WebStream = _WebResponse.GetResponseStream(); 

       // convert webstream to image 
       _tmpImage = Image.FromStream(_WebStream); 

       // Cleanup 
       _WebResponse.Close(); 
       _WebResponse.Close(); 
      } 
      catch (Exception _Exception) 
      { 
       // Error 
       Console.WriteLine("Exception caught in process: {0}", _Exception.ToString()); 
       return null; 
      } 

      return _tmpImage; 
     } 
+0

당신은 다운로드해야합니다 디렉토리 목록을 작성하고 파싱하여 파일의 URL을 가져옵니다. 구문 분석을 위해 HTML 구문 분석기를 사용해 볼 수도 있지만 정규식과 같은 간단한 것을 사용하면 여기에서도 작동 할 수 있습니다. – svick

답변

관련 문제