C#远程获取/读取网页内容

<script type="text/JavaScript"> alimama_pid="mm_10249644_1605763_4929893"; alimama_titlecolor="0000FF"; alimama_descolor ="000000"; alimama_bgcolor="FFFFFF"; alimama_bordercolor="E6E6E6"; alimama_linkcolor="008000"; alimama_bottomcolor="FFFFFF"; alimama_anglesize="0"; alimama_bgpic="0"; alimama_icon="0"; alimama_sizecode="16"; alimama_width=658; alimama_height=60; alimama_type=2; </script> <script src="http://a.alimama.cn/inf.js" type=text/javascript> </script>

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.IO;
  6. namespace thief
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             
  13.             try {
  14.                 WebClient MyWebClient = new WebClient();
  15.                 MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于对向Internet资源的请求进行身份验证的网络凭据。
  16.                 Byte[] pageData = MyWebClient.DownloadData(http://www.163.com); //从指定网站下载数据
  17.                 string pageHtml = Encoding.Default.GetString(pageData);  //如果获取网站页面采用的是GB2312,则使用这句              
  18.                 //string pageHtml = Encoding.UTF8.GetString(pageData); //如果获取网站页面采用的是UTF-8,则使用这句
  19.                 Console.WriteLine(pageHtml);//在控制台输入获取的内容
  20.                 using (StreamWriter sw = new StreamWriter("c://test//ouput.html"))//将获取的内容写入文本
  21.                 {
  22.                     sw.Write(pageHtml);
  23.                 }
  24.                 Console.ReadLine(); //让控制台暂停,否则一闪而过了               
  25.             }
  26.             catch(WebException webEx) {
  27.                 Console.WriteLine(webEx.Message.ToString());
  28.             }
  29.         }
  30.     }
  31. }
改进一下,加入定时器后
  1. using System;
  2. using System.Text;
  3. using System.Timers;
  4. using System.Net;
  5. using System.IO;
  6. //<summary>
  7. //每隔5秒钟将指定网页的内容抓取下来,并以文件形式保存到c:/test目录中
  8. //</summary>
  9. namespace TimerTest
  10. {
  11.     class Program
  12.     {
  13.         public static string outFileName = "";                  //生成的文件名
  14.         public static string myUrl = "http://bxg.cfchina.cn";    //要抓取的网页
  15.         static void Main(string[] args)
  16.         {
  17.             Timer mytimer = new Timer();
  18.             mytimer.Elapsed +=new ElapsedEventHandler(GetUrl);//指定定时器的事件
  19.             mytimer.Interval = 5000;//每隔5秒抓一次
  20.             mytimer.Start();
  21.             mytimer.Enabled = true;
  22.             while (Console.Read() != 'q'//直到按小写字母q退出,否则一直抓取下去
  23.             {
  24.             }
  25.         }
  26.         //定时器事件内容
  27.         static void GetUrl(object source, ElapsedEventArgs e)
  28.         {
  29.             try
  30.             {
  31.                 WebClient MyWebClient = new WebClient();
  32.                 MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于对向Internet资源的请求进行身份验证的网络凭据。
  33.                 Byte[] pageData = MyWebClient.DownloadData(myUrl);//从指定网站下载数据
  34.                 string pageHtml = Encoding.Default.GetString(pageData);  //如果获取网站页面采用的是GB2312,则使用这句              
  35.                 //string pageHtml = Encoding.UTF8.GetString(pageData); //如果获取网站页面采用的是UTF-8,则使用这句
  36.                 //Console.WriteLine(pageHtml);//在控制台输入获取的内容
  37.                 outFileName = "C://test//" + DateTime.Now.ToString().Replace(" ", "").Replace(":", "").Replace("-", "") + ".html";
  38.                 using (StreamWriter sw = new StreamWriter(outFileName))//将获取的内容写入文本
  39.                 {
  40.                     sw.Write(pageHtml);
  41.                 }
  42.                 Console.WriteLine(outFileName);  //输出保存后的文件名                      
  43.             }
  44.             catch (WebException webEx)
  45.             {
  46.                 Console.WriteLine(webEx.Message.ToString());
  47.             }
  48.         }
  49.     }
  50. }
  51. <script type="text/JavaScript"> alimama_pid="mm_10249644_1605763_5027492"; alimama_type="f"; alimama_sizecode ="tl_1x5_8"; alimama_fontsize=12; alimama_bordercolor="FFFFFF"; alimama_bgcolor="FFFFFF"; alimama_titlecolor="0000FF"; alimama_underline=0; alimama_height=22; alimama_width=0; </script> <script src="http://a.alimama.cn/inf.js" type=text/javascript> </script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值