1. 程式人生 > >C# 採集當前頁面資料並儲存

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, @"(\<script(.+?)\</script\>)|(\<style(.+?)\</style\>)", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                int iBodyStart = pageHtml.IndexOf("<body", 0);
                int iStart = pageHtml.IndexOf("<main", iBodyStart);
                int iTableStart = pageHtml.IndexOf("<article", iStart);
                int iTableEnd = pageHtml.IndexOf("</article>", 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());
            }
        }