/// 柳永法采集类

来源:互联网 发布:外文原版图书数据库 编辑:程序博客网 时间:2024/06/05 07:17
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Text;   
  4. using System.Diagnostics;   
  5. using System.Text.RegularExpressions;   
  6. using System.IO;   
  7. using System.Net;   
  8.   
  9. namespace 采集测试   
  10. {   
  11.     class Program   
  12.     {   
  13.         static void Main(string[] args)   
  14.         {   
  15.             string[] urls= {   
  16.                 "http://www.yongfa365.com",   
  17.                 "http://www.cbdcn.com",   
  18.                 "http://www.csdn.net",   
  19.                 "http://www.sina.com",   
  20.                 "http://www.tom.com",   
  21.             };   
  22.   
  23.             string html="";   
  24.   
  25.             html = CaiJi.GetHtmlSource("http://www.yongfa365.com", Encoding.Default);   
  26.             Console.Write(html);   
  27.             Console.ReadKey();   
  28.   
  29.             html = CaiJi.GetHtmlSource("http://www.baidu.com/");   
  30.             Console.Write(html);   
  31.             Console.ReadKey();   
  32.   
  33.             html = CaiJi.GetHtmlSource("http://www.tom.com","utf-8");   
  34.             Console.Write(html);   
  35.             Console.ReadKey();   
  36.   
  37.             foreach (string url in urls)   
  38.             {   
  39.                 Console.Write(CaiJi.GetHtmlSource(url));   
  40.                 Console.ReadKey();   
  41.                
  42.             }   
  43.         }   
  44.     }   
  45. }   
  46.   
  47.   
  48. /// <summary>   
  49. /// 柳永法采集类   
  50. /// </summary>   
  51. class CaiJi   
  52. {   
  53.     /// <summary>   
  54.     /// 取得网页源码   
  55.     /// </summary>   
  56.     /// <param name="url">网页地址,eg:"http://www.yongfa365.com/" </param>    
  57.     /// <param name="charset">网页编码,eg:"utf-8"</param>   
  58.     /// <returns>返回网页源文件</returns>   
  59.     public static string GetHtmlSource(string url, string charset)   
  60.     {   
  61.         //编码处理    
  62.         Encoding nowCharset;   
  63.         if (charset == "" || charset == null)   
  64.         {   
  65.             nowCharset = Encoding.Default;   
  66.         }   
  67.         else  
  68.         {   
  69.             nowCharset = Encoding.GetEncoding(charset);   
  70.         }   
  71.   
  72.         //处理内容   
  73.         string html = "";   
  74.         try  
  75.         {   
  76.             //WebRequest myWebRequest = WebRequest.Create(url);   
  77.             //WebResponse myWebResponse = myWebRequest.GetResponse();   
  78.             //Stream stream = myWebResponse.GetResponseStream();   
  79.             //StreamReader reader = new StreamReader(stream, nowCharset);   
  80.   
  81.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);   
  82.             HttpWebResponse response = (HttpWebResponse)request.GetResponse();   
  83.             Stream stream = response.GetResponseStream();   
  84.             StreamReader reader = new StreamReader(stream, nowCharset);   
  85.             html = reader.ReadToEnd();   
  86.             stream.Close();   
  87.         }   
  88.         catch (Exception e)   
  89.         {   
  90.         }   
  91.         return html;   
  92.     }   
  93.   
  94.     /// <summary>   
  95.     /// 取得网页源码   
  96.     /// </summary>   
  97.     /// <param name="url">网页地址,eg: "http://www.yongfa365.com/" </param>    
  98.     /// <param name="charset">网页编码,eg: Encoding.UTF8</param>   
  99.     /// <returns>返回网页源文件</returns>   
  100.     public static string GetHtmlSource(string url, Encoding charset)   
  101.     {   
  102.         //处理内容   
  103.         string html = "";   
  104.         try  
  105.         {   
  106.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);   
  107.             HttpWebResponse response = (HttpWebResponse)request.GetResponse();   
  108.             Stream stream = response.GetResponseStream();   
  109.             StreamReader reader = new StreamReader(stream, charset);   
  110.             html = reader.ReadToEnd();   
  111.             stream.Close();   
  112.         }   
  113.         catch (Exception e)   
  114.         {   
  115.         }   
  116.         return html;   
  117.     }   
  118.   
  119.     /// <summary>   
  120.     /// 取得网页源码   
  121.     /// 对于带BOM的网页很有效,不管是什么编码都能正确识别   
  122.     /// </summary>   
  123.     /// <param name="url">网页地址,eg: "http://www.yongfa365.com/" </param>    
  124.     /// <returns>返回网页源文件</returns>   
  125.     public static string GetHtmlSource(string url)   
  126.     {   
  127.         //处理内容   
  128.         string html = "";   
  129.         try  
  130.         {   
  131.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);   
  132.             HttpWebResponse response = (HttpWebResponse)request.GetResponse();   
  133.             Stream stream = response.GetResponseStream();   
  134.             StreamReader reader = new StreamReader(stream, Encoding.Default);    
  135.             html = reader.ReadToEnd();   
  136.             stream.Close();   
  137.         }   
  138.         catch (Exception e)   
  139.         {   
  140.         }   
  141.         return html;   
  142.     }   
  143. }