asp.net C# 擷取網頁源碼的幾種方式

來源:互聯網
上載者:User

標籤:webbrowser   源碼   c#   asp.net   

1 方法
               System.Net.WebClient aWebClient = new System.Net.WebClient();               aWebClient.Encoding = System.Text.Encoding.Default;               Byte[] pageData = aWebClient.DownloadData(url);                          string nhtml = Encoding.GetEncoding("utf-8").GetString(pageData);

2方法

                System.Net.WebClient aWebClient = new System.Net.WebClient();                aWebClient.Encoding = System.Text.Encoding.Default;                string nhtml = aWebClient.DownloadString(goodstidurl);

3方法
               WebBrowser webbrowser = new WebBrowser();                StreamReader sr = new StreamReader(this.webBTaobao.DocumentStream, Encoding.Default);                html = sr.ReadToEnd();                html = html.Replace("\r\n", "");                html = html.Replace("\n", "");                html = html.Replace("  ", "");                html = html.Replace("(", "");                html = html.Replace(")", "");                string nurl = Regex.Match(html, "(?<=data-url=\").*?(?=\")").Value;                //建立一個WebBrowser                WebBrowser webAddress = new WebBrowser();                webAddress.Navigate(nurl);                //等待載入完成                while (webAddress.ReadyState < WebBrowserReadyState.Complete) Application.DoEvents();                StreamReader sraddress = new StreamReader(webAddress.DocumentStream, Encoding.Default);                jsonaddress = sraddress.ReadToEnd();

4方法

            WebRequest hwr = WebRequest.Create(@"http://item.taobao.com/item.htm?id=" + row["urlId"].ToString());//向指定Url發出請求             HttpWebResponse hwp = hwr.GetResponse() as HttpWebResponse;//將hwr對HTTP的請求             string text;            StreamReader sr;            string code = hwp.ContentType;//請求響應得到的內容類型             //得到編碼了             code = code.Split('=')[1];            Stream rep = hwp.GetResponseStream();//將請求得到的內容以流的形式讀出             sr = new StreamReader(rep, Encoding.GetEncoding(code));//用指定的字元編碼為指定的流初始化 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.