Mopaka~

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

大热天气闷死了,没事写个简单的网页采集程序(超级简单版)

protected void btn_click(object sender, EventArgs e)
        {
            //方法一:
            //System.Net.WebClient wc = new System.Net.WebClient();
            //byte[] b = wc.DownloadData("http://www.baidu.com");
            //string html = System.Text.Encoding.GetEncoding("gb2312").GetString(b);
            //html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
            //html = html.Substring(0, html.IndexOf("</p>"));
            //Response.Write(html);

            //方法二:
        //获取整个网页
            System.Net.WebClient wc = new System.Net.WebClient();
            System.IO.Stream sm = wc.OpenRead("http://www.baidu.com");
            System.IO.StreamReader sr = new System.IO.StreamReader(sm, System.Text.Encoding.Default, true, 256000);
            string html = sr.ReadToEnd();
            sr.Close();
            //根据规则获取想要的内容
            html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
            html = html.Substring(0, html.IndexOf("</p>"));
            Response.Write(html);
        }
posted on 2010-06-30 21:31  戴海江  阅读(829)  评论(1编辑  收藏  举报