Python automatically obtains the weather program (V1)

Source: Internet
Author: User

1. Functions of the program

Run the program to automatically obtain the current city of the user and display the latest weather information of the city.

2. Procedure

1) obtain the user's City

2) city names are simplified, for example, "Hangzhou City, Zhejiang Province" is converted to "Hangzhou" for convenient query.

3) Search for the city number through the simplified city name (the number is used to capture the weather of the city on the website)

4) capture the city weather (connected to the http://www.weather.com.cn/data/sk/cityID.html to obtain the weather information, and then extract the specific weather information from the returned information

3. program description

1. The appGetWeather_help.py module implements the main conversion and weather retrieval functions.

2. The main appGetWeather. py module implements the program process and obtains the city.

4. Code V1

AppGetWeather. py main module

#! /Usr/bin/env python # coding = UTF-8 # Author: JarvisChu # Blog: blog.csdn.net/jarvischuimport sys, urllib2, appGetWeather_help # Locate the citycity_info = urllib2.urlopen ('HTTP: // response '). read () city = city_info.split ('=') [1]. split (',') [2]. split ('"') [3] # split out the city name ''' print u" your city :", city # region # convert city name to shortcityName = appGetWeather_help.convertName (city) # print cityName # convert city name to cityIDcityID = province (cityName) # print "cityId :", cityID # region # get the weathermsg = appGetWeather_help.getCityWeather_RealTime (cityID) print U' city: ', msg ['city'] print U' temperature :', msg ['temp '] print U' wind direction:', msg ['wind '] print U' humidity:', msg ['sd'] print U' Update Time :', msg ['time']

AppGetWeather_help.py Module

#! /Usr/bin/env python # coding = UTF-8 # Author: JarvisChu # Blog: blog.csdn.net/jarvischu#import OS, io, re, time, base64import sys, urllib2cityList_main = [# major cities in China # North China {'code': "101010100", 'name': "Beijing"}, {'code': "101030100", 'name ': "Tianjin" },{ 'code': "101090101", 'name': "Shijiazhuang" },{ 'code': "101100101", 'name ': "Taiyuan" },{ 'code': "101080101", 'name': "Hohhot" },{ 'code': "101090201", 'name ': "Baoding"}, {'code': "101100201", 'name': "Datong" },{ 'code': "101080201", 'name': "Baotou" },{ 'code': "101090402", 'name ': "Chengde" },{ 'code': "101100401", 'name': "Jinzhong" },{ 'code': "101080501", 'name ': "Tongliao" },{ 'code': "101091101", 'name': "Qinhuangdao" },# northeast {'code': "101050101", 'name ': "Harbin" },{ 'code': "101060101", 'name': "Changchun" },{ 'code': "101070101", 'name ': "Shenyang" },{ 'code': "101050201", 'name': "Qiqihar" },{ 'code': "101060201", 'name ': "Jilin"}, {'code': "101070201", 'name': "Dalian "}, {'Code': "101050301", 'name': "Mudanjiang"}, {'code': "101060301", 'name': "Yanji "}, {'code': "101070301", 'name': "Anshan"}, {'code': "101050501", 'name': "Suihua "}, {'code': "101060601", 'name': "White City"}, {'code': "101071401", 'name': "Huludao "}, # South China {'code': "101280101", 'name': "Guangzhou" },{ 'code': "101300101", 'name': "Nanning "}, {'code': "101310101", 'name': "Haikou" },{ 'code': "101320101", 'name': "Hong Kong "}, {'code': "101330101", 'name': "Macao"}, {'code': "101280601", 'name': "Shenzhen" },{ 'code': "101300501", 'name': "Guilin" },{ 'code ': "101310201", 'name': "Sanya" },{ 'code': "101280701", 'name': "Zhuhai" },{ 'code': "101281701 ", 'name': "Zhongshan" },{ 'code': "101301001", 'name': "Baise" },{ 'code': "101310215", 'name ': "Wanning" },# northwest {'code': "101110101", 'name': "Xi'an" },{ 'code': "101160101", 'name ': "Lanzhou" },{ 'code': "101150101", 'name': "Xining" },{ 'code': "101170101", 'name ': "Yinchuan" },{ 'code': "101130101 ", 'Name':" Urumqi "},{ 'code':" 101110300 ", 'name':" yan'an "},{ 'code':" 101110901 ", 'name': "Baoji"}, {'code': "101160901", 'name': "Tianshui"}, {'code': "101170301", 'name ': "Wu Zhong" },{ 'code': "101130501", 'name': "Turpan" },{ 'code': "101160801", 'name ': "Jiuquan" },{ 'code': "101170401", 'name': "Gu yuan" },# southwest {'code': "101040100", 'name ': "Chongqing" },{ 'code': "101270101", 'name': "Chengdu" },{ 'code': "101260101", 'name ': "Guiyang"}, {'code': "101290101", 'name ': "Kunming" },{ 'code': "101140101", 'name': "Lhasa" },{ 'code': "101270401", 'name ': "Mianyang" },{ 'code': "101260201", 'name': "Zunyi" },{ 'code': "101290201", 'name ': "Dali" },{ 'code': "101271401", 'name': "Leshan" },{ 'code': "101260801", 'name ': "liupanshui" },{ 'code': "101291401", 'name': "Lijiang" },# East China {'code': "101020100", 'name ': "Shanghai" },{ 'code': "101230101", 'name': "Fuzhou" },{ 'code': "101220101", 'name ': "Hefei"}, {'code': "101240101", 'name': "Nanchang "},{ 'Code': "101120101", 'name': "Jinan"}, {'code': "101210301", 'name': "Jiaxing"}, {'code ': "101190101", 'name': "Nanjing" },{ 'code': "101210401", 'name': "Ningbo" },{ 'code': "101210101 ", 'name': "Hangzhou" },{ 'code': "101190401", 'name': "Suzhou" },{ 'code': "101120201", 'name ': "Qingdao" },{ 'code': "101230201", 'name': "Xiamen" },{ 'code': "101340101", 'name ': "Taipei City" },# Huazhong {'code': "101180101", 'name': "Zhengzhou" },{ 'code': "101200101", 'name ': "Wuhan" },{ 'code': "10 1250101 ", 'name':" Changsha "},{ 'code':" 101180201 ", 'name':" Anyang "},{ 'code':" 101200201 ", 'name': "Xiangyang"}, {'code': "101250201", 'name': "Xiangtan"}, {'code': "101250301", 'name ': "Zhuzhou" },{ 'code': "101180401", 'name': "xuchang" },{ 'code': "101250601", 'name ': "Changde" },{ 'code': "101251101", 'name': "Zhangjiajie" },{ 'code': "101200401", 'name ': "Xiaogan" },{ 'code': "101201401", 'name': "Jingmen"}] ''' {"weatherinfo": {"city": "Beijing ", "cityid": "101010100", "temp": "2 5 "," WD ":" Dongfeng "," WS ":" Level 2 "," SD ":" 50% "," WSE ":" 2 "," time ": "20:30", "isRadar": "1", "Radar": "JC_RADAR_AZ9010_JB"} ''' # Return dict type: twitter = {'city': city, 'temp ': temperature} def getCityWeather_RealTime (cityID): url = "http://www.weather.com.cn/data/sk/" + str (cityID) + ". html "try: req = urllib2.Request (url) stdout = urllib2.urlopen (url) weatherInfomation = stdout. read (). decode ('utf-8') msg ={} msg ['city'] = weathe RInfomation. split ('"') [5] msg ['temp '] = weatherInfomation. split ('"') [13] msg ['wind '] = weatherInfomation. split ('"') [17] +'' + weatherInfomation. split ('"') [21] msg ['sd'] = weatherInfomation. split ('"') [25] msg ['time'] = weatherInfomation. split ('"') [33] failed T (SyntaxError) as err: print (" >>>>> SyntaxError: "+ err. args) Counter T: print (">>>>> OtherError:") else: return msg finally: None def getCityCodeFromNa Me (cityName): for item in cityList_main: # n = unicode (item ['name'], 'utf-8') # print n, cityName if unicode (item ['name'], 'utf-8') = cityName: # print 'equal' return item ['code'] return ''' def convertName (cityName): # convert "Hangzhou City, Zhejiang Province" to "Hangzhou" # print 'convertname :', cityName name = unicode (cityName, "gbk") if name. find (u "Province ")! =-1: # contains 'province '# print U' with province 'name = name. split (u'province ') [1] if name. find (u "city ")! =-1: # contains 'city' # print U' city name = name. split (u'city') [0] return name

City code in the code, directly using the http://www.oschina.net/code/snippet_734886_13619

5. Running Effect

Repost this article to indicate the author and Source

Author: JarvisChu

Source: http://blog.csdn.net/jarvischu

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.