Python parses weather data of China weather network

Source: Internet
Author: User

Usage: Input in terminal
Copy codeThe Code is as follows: python weather. py http://www.weather.com.cn/weather/101010100.shtml

Json format of 6-day weather data in Beijing

Copy codeThe Code is as follows: # coding = UTF-8
# Weather. py
Import urllib
Import re
Import simplejson
Import sys

If len (sys. argv )! = 2:
Print 'Please enter: python' + sys. argv [0] + '<url>'
Exit (0)
Url = sys. argv [1];
Def readurlPageContent (url): # obtain all data on the page and read it row by row.
Webpage = urllib. urlopen (url );
Line = webpage. readline ();
Data =''
While line:
Data = data + line. strip (); # Remove spaces on both sides of each line
Line = webpage. readline ();
Return data

Def getDatabody (data ):
Reg = re. compile (R' (<div class = \ "weatherYubaoBox \"> (? :(?! <Div )(?! </Div).) * </div> )');
Matchs = reg. findall (data, re. I );
If len (matchs)> 0:
Return matchs [0]
Return None

Def getSixDayWeather (data ):
Regs = re. compile (R'> ((? :(?! <) (?!>) (?! Var).) +) <');
Datas = regs. findall (data)
If len (datas)> 12:
Datas = datas [12: len (datas)-7]

Return datas
Return None


Data = readurlPageContent (url );
Match_data = getDatabody (data)
If match_data = None:
Print 'get weather data fail'
Exit (0)

Weathers_data = getSixDayWeather (match_data)
If weathers_data = None:
Print 'get six day info fail'
Exit (0)

Count = len (weathers_data );
Groups_item_count = count/6;
Weathers = {};
Groups = [];

Start = 0
For item in weathers_data:
If (start % groups_item_count) = 0:
Groups = [];
Weathers [('day' + str (start/groups_item_count + 1)] = groups;

Groups. append (item)
Start = start + 1


Print simplejson. dumps (weathers, encoding = 'utf-8', ensure_ascii = False)

Note: The third-party JSON library simplejson is used in this article. The installation method can be found below:
1. Download: http://pypi.python.org/pypi/simplejson/, the file is compressed
2. Unzip: Right-click to decompress the file. For example, D:/simplejson.
3. Start --- run --- Enter command: cmd
4. Go to the directory (for example, D:/simplejson)
Copy codeThe Code is as follows: cd D:/simplejson
5. Run the Installation File: setup. py install

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.