C# 采集当前页面数据并保存

 private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                WebClient MyWebClient = new WebClient();
                MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据
                Byte[] pageData = MyWebClient.DownloadData("https://blog.csdn.net/dQCFKyQDXYm3F8rB0/article/details/82990803"); //从指定网站下载数据
               // string pageHtml = Encoding.Default.GetString(pageData);
                string pageHtml = Encoding.UTF8.GetString(pageData);  //如果获取网站页面采用的是UTF-8,则使用这句
                                                                     
                pageHtml = Regex.Replace(pageHtml, @"(\)|(\)", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                int iBodyStart = pageHtml.IndexOf("", iTableStart);
                string strWeb = pageHtml.Substring(iTableStart, iTableEnd - iTableStart + 10);
                //删除标签
                var r = new Regex(@"]*>", RegexOptions.IgnoreCase);
                Match m;
                for (m = r.Match(strWeb); m.Success; m = m.NextMatch())
                {
                    strWeb = strWeb.Replace(m.Groups[0].ToString(), "");
                }
                Console.WriteLine(strWeb);//在控制台输入获取的内容
                using (StreamWriter sw = new StreamWriter("c:\\test\\ouput.html"))//将获取的内容写入文本
                {
                    sw.Write(strWeb);
                }
                Console.ReadLine();
            }
            catch (WebException webEx)
            {
                Console.WriteLine(webEx.Message.ToString());
            }
        }

 

你可能感兴趣的:(C#)