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