IP get weather forecast (1) --- data analysis and storage

Source: Internet
Author: User
    • In the past few days, I plan to build my favorite weather forecast system. Every time I browse the Web page, I can see the weather in different cities according to IP address changes. After two days of efforts and attempts, there was little progress. These mainly involve:
    1. HtmlCodeAnalyze and obtain the Pinyin and location codes of all cities not provided by all websites.
    2. IP address calculation, and get the Pinyin and city names from the database.
    3. Analyze the XML code and Dispart the data to obtain timely display of official data not provided in XML.
    4. Create your own RSS subscription service.
    • First look at the effect or preview: Address

    • Idea: access the page ---> get the visitor's IP ---> calculate the IP address as an integer ---> then obtain the city corresponding to the IP address from the database, city pinyin ---> analyze HTML to extract Yahoo weather data ---> get the code region through pinyin ---> then analyze XML and HTML to get the desired content.
    • First, you must find a website that can provide services. I found Yahoo weather. In brief, you need to enter the Pinyin of the city in the weather and go to the corresponding page. For example, in Beijing, enter Beijing to get the code specified by Beijing in Yahoo weather, chxx0008. According to my existing information on the Internet, one is the IP address and the provincial capital of the city, and the other is the capital city and the city pinyin. So you must get the code to get the weather.
    • On the http://weather.yahoo.com/regional/CHXX.html page, we can see that the list of all cities is regular. We can use htmldocument to obtain the address of the row of each link in the list under each letter. After analyzing the address, we can propose the Pinyin of the city name and the code behind chxx.
    • This requires a winformProgram. Delegates an event to asynchronously process the event.

  •  
     
     Code 
    Private Void Form1_load ( Object Sender, eventargs E)
    {
    String Auila = " Http://weather.yahoo.com/regional/CHXX.html " ;
    String Aurl = String . Empty;
    Aurl = Auila;
    Uri auri = New Uri (aurl );
    Webbrowser abrowser = New Webbrowser ();
    Abroted. documentcompleted + = New Webbrowserdocumentcompletedeventhandler (onlistcompleted );
    Abrowser. url = Auri;
    }
  • After carefully reading the code in HTML, we can find that all href stores the link addresses we need. What can we do if we can locate them? It is found that all links are listed in Li, and the previous ID exactly corresponds to all contents of this Li. Use getelementbyid () in htmlelement to locate the desired ID. Then, we can gradually analyze Li and extract them cyclically to extract all the names and links to an array. Store the data directly in your own database.
  •     Using  (Webbrowser abrowser  =  Sender As  Webbrowser)
    {
    If (Abrowser ! = Null )
    {
    Htmldocument adoc = Abrow.document;
    Htmlelement aliste = Adoc. getelementbyid ( " ***** " ); // Locate the analysis position
    Htmlelementcollection ACOl = Aliste. getelementsbytagname ( " Li " );
    Foreach (Htmlelement adataitem In ACOl)
    {
    Try
    {
    // Analyze and process the corresponding HTML code.
    // Obtain the result through loop Processing
    //
    //
    String Namecn = Alink. innerhtml;
    Createnewcode (codech, namecn );
    }
    Catch (Exception ex)
    {}
    }
    }
  • The extracted results are as follows. There are about 500 records.
  • Summary: There are so many data extraction issues. If the online data source stores IP addresses and cities, you can find them. I found an access version. The following section describes IP Address Calculation and extraction.
  • Test address
  • Simply add a link directory. If you are interested, you can study it together :)
  • IP get weather forecast (1) --- data analysis and storage
  • IP get weather forecast (2) --- IP calculation and Data Processing
  • IP get weather forecast (3) --- CDATA extraction in XML
  • IP get weather forecast (4) --- make your own RSS subscription

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.