Query about weather in China (Weather.com.cn)

Source: Internet
Author: User

I have read about how to obtain the weather forecast over the past two days. I will write an article to summarize it:

Weather.com has the following three Query Interfaces in Json format:

  1. Http://www.weather.com.cn/data/sk/101010100.html
  2. Http://www.weather.com.cn/data/cityinfo/101010100.html
  3. Http://m.weather.com.cn/data/101010100.html

You can directly access it to obtain data in Json format and use it after parsing. Among them, the third m.weather.com.cn has the most comprehensive information, and the others have enough weather forecasts for the day. To obtain the weather conditions of other cities, you only need to change the red city ID to another ID. The following describes how to obtain the city ID.

ID is a 9-digit number. The length can be divided into the following four parts:

101 (country code)
01 (province)
01 (level 2 region)
00 (level 3 region)

The code 101 of the first three countries is fixed and does not need to be obtained. The key is to obtain the third-level region code. To obtain code for all regions, you can obtain the code from the following address:

Http://m.weather.com.cn/data5/city0101.xml

It has a parameter, which is the region code and returns the region code of the next level. In addition, although the uri format is xml, the returned result is not xml, but similar to "01 | Beijing, 02 | Shanghai, 03 | Tianjin, 04 | Chongqing "formatted string, but it is well parsed.

To obtain all region codes, use the following method:

  1. Visit http://m.weather.com.cn/data5/city.xmlto get the number of all provinces (example: 01 | Beijing, 02 | Shanghai, 03 | Tianjin)
  2. Obtain the IDs of all level 2 regions based on the province ID. For example, visit http://m.weather.com.cn/data5/city01.xml.
  3. Access Level 3 by Level 2, for example, http://m.weather.com.cn/data5/city0101.xml.

With the above foundation, we can simply write a small program to get the IDs of all cities.

Class
CityInfo
{
Public
String Name {get; private
Set ;}
Public
String Id {get; private
Set ;}

Private CityInfo () // This object cannot be created externally
{
}

Public
Override
String ToString ()
{
Return
String. Format ("[{0}, {1}]", Name, Id );
}

Static
HttpClient httpClient = new
HttpClient ();

Async
Task <CityInfo []> GetSubCitiesAsync ()
{
Var requestUrl = string. Format ("http://m.weather.com.cn/data5/city%0%.xml", Id );
Var result = await httpClient. GetStringAsync (requestUrl );

Var cityInfo = from city in result. Split (',')
Let data = city. Split ('| ')
Select
New
CityInfo () {Id = data [0], Name = data [1]};

Return cityInfo. ToArray ();
}

Static
Async
Task <IEnumerable <CityInfo> GetSubCitiesAsync (IEnumerable <CityInfo> cities)
{
Var tasks = cities. Select (I => I. GetSubCitiesAsync ());
Await
Task. WhenAll (tasks );

Return tasks. Select (I => I. Result). SelectMany (I => I );
}

Public
Static
Async
Task <IEnumerable <CityInfo> GetAllCitiesAsync ()
{
Var root = new
CityInfo (); // The null object is a level 0 object.
Var level1Cities = await root. GetSubCitiesAsync ();
Var level2Cities = await GetSubCitiesAsync (level1Cities );
Var level3Cities = await GetSubCitiesAsync (level2Cities );

Return level3Cities;
}
}

This class is very easy to use. It has only one interface GetAllCitiesAsync (), through which the IDs of all level-3 cities can be returned.

Foreach (var item in
Await
CityInfo. GetAllCitiesAsync ())
{
Console. WriteLine (item );
}

Since the speed of obtaining depends mainly on the network, the logic here is relatively simple and I have not deliberately pursued the highest efficiency. After a simple test, we can return results about 20 s, with a total of 2564 cities. It seems that the list of city IDs on the internet is incomplete.

For the city list, I will not post it here. If you need it, just run the code above (the. net 4.5 environment is required ).

 

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.