C # Crawl data on a Web page

Source: Internet
Author: User

recent job demands regularly crawl the temperature of different cities every day. In fact, through the method of programming to crawl different Web pages for analysis and screening process. NET provides many classes to access and obtain data for remote Web pages, such as the WebClient class and the HttpWebRequest class. These classes are useful for using HTTP to access remote Web pages and downloading them, but it is very weak in terms of the parsing capabilities of the downloaded HTML. Recommend an open Source component HTML Agility Pack (http://htmlagilitypack.codeplex.com/), which is designed to simplify the reading and writing of HTML documents as much as possible. The package itself uses the DOM document Object model to parse the HTML. In this way record a recent collection of crawl history and current weather for the site backup:

    • Support Historical Weather query in Chinese website: http://lishi.tianqi.com/
    • Similar sites: http://tianqi.2345.com/wea_history/59287.htm (last 2-3 years data)
    • Website: http://www.wunderground.com/history/(last 18 years data)
    • This site is supported for a longer period of time: http://www.tutiempo.net/en/Climate/

Examples of programming use are: we want to get weather information from the following pages:

Download the HTML Agility Pack component, create a new console program, reference the corresponding framework version of the component in your project, the sample code is as follows:         

stringURL =@"http://lishi.tianqi.com/beijing/201701.html"; varWebGet =NewHtmlweb (); varDocument =webget.load (URL); vardiv = document. Documentnode.selectnodes ("//div[@class = ' tqtongji2 ']/ul"); foreach(Htmlnode nodeinchDiv) {varTmpnode = node. SelectNodes ("Li"); Console.WriteLine (string. Format ("{ 0}-----------{1}---------{2}----------{3}", tmpnode[0]. InnerText, tmpnode[1]. InnerText, tmpnode[2]. InnerText, tmpnode[3].            InnerText)); } console.readkey ();

Program running effect: Chinese is garbled, such as

By parsing the HTML Agility pack source code, in the Htmlweb class get (Uri Uri, string method, String path, HTMLDocument doc) method, the local variable RESP is the response of the HTTP request. Set breakpoints to discover resp. ContentEncoding is empty. So download the data via HttpWebRequest, the sample code is as follows:

stringURL =@"http://lishi.tianqi.com/beijing/201701.html"; HttpWebRequest req= WebRequest.Create (NewUri (URL)) asHttpWebRequest; Req. Method="GET"; WebResponse rs=req.            GetResponse (); Stream RSS=Rs.            GetResponseStream (); HTMLDocument Doc=NewHTMLDocument (); Doc.            Load (RSS); vardiv = doc. Documentnode.selectnodes ("//div[@class = ' tqtongji2 ']/ul"); foreach(Htmlnode nodeinchDiv) {varTmpnode = node. SelectNodes ("Li"); Console.WriteLine (string. Format ("{ 0}-----------{1}---------{2}----------{3}", tmpnode[0]. InnerText, tmpnode[1]. InnerText, tmpnode[2]. InnerText, tmpnode[3].            InnerText)); } console.readkey ();

The code works as follows:

That ' s OK!!!

C # Crawl data on a Web page

Related Article

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.