C # crawl Web page HTML content

Source: Internet
Author: User

Online a lot of content collection tools, today I try to write one, found that C # can easily grasp the content of the Web page, and then through the regular to separate their own interesting data. Here is the code to grab the Web content:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Net;usingSystem.Text;usingSystem.IO;usingSystem.Text.RegularExpressions;namespaceweb{/// <summary>     ///Public Method Classes/// </summary>      Public classWebHandler {/// <summary>        ///get HTML code for a Web page/// </summary>        /// <param name= "url" >Link Address</param>        /// <param name= "encoding" >Encoding Type</param>        /// <returns></returns>         Public Static stringGETHTMLSTR (stringUrlstringencoding) {         stringHtmlstr =""; Try         {            if(!string.isnullorempty (URL)) {WebRequest Request= WebRequest.Create (URL);//instantiating a WebRequest objectWebResponse response = Request. GetResponse ();//Create a WebResponse objectStream datastream = Response. GetResponseStream ();//creating a Stream objectEncoding EC =Encoding.default; if(Encoding = ="UTF8") {EC=Encoding.UTF8; }               Else if(Encoding = ="Default") {EC=Encoding.default; } StreamReader Reader=NewStreamReader (datastream, EC); Htmlstr= Reader. ReadToEnd ();//read Web page contentReader.               Close (); DataStream.               Close (); Response.            Close (); }         }         Catch { }         returnHtmlstr; }   }     }

This method can get the HTML content of the Web page, with HTML we can use the regular to grab the content we want ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.