1. 程式的功能
運行程式,自動擷取使用者當前的城市,然後顯示城市的最新天氣資訊
2. 程式流程
1)擷取使用者所在的城市
2)城市名簡化,如“浙江省杭州市”轉化為‘杭州’,方便查詢
3)通過簡化的城市名尋找到該城市的編號(編號用於在網站上抓取該城市的天氣)
4)抓取城市天氣(串連 http://www.weather.com.cn/data/sk/cityID.html 擷取天氣資訊 ,然後從返回的資訊中提取出具體的天氣資訊
3. 程式說明
1. appGetWeather_help.py 模組,實現了主要的轉換和擷取天氣的功能
2. appGetWeather.py 主模組,實現了程式的流程,還有城市的擷取
4. 代碼 V1
appGetWeather.py 主模組
#! /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://pv.sohu.com/cityjson').read() city = city_info.split('=')[1].split(',')[2].split('"')[3] #split out the city name'''print u"您的城市:",city#------------------------------------------------------------------------------#convert city name to shortcityName=appGetWeather_help.convertName(city)#print cityName#convert city name to cityIDcityID=appGetWeather_help.getCityCodeFromName(cityName)#print "cityId:",cityID#---------------------------------------------------#get the weathermsg = appGetWeather_help.getCityWeather_RealTime(cityID)print u'城市: ',msg['city']print u'氣溫: ',msg['temp']print u'風向: ',msg['wind']print u'濕度: ',msg['sd']print u'更新時間:',msg['time']
appGetWeather_help.py 模組
#! /usr/bin/env python#coding=utf-8#Author: JarvisChu#Blog: blog.csdn.net/jarvischu#import os, io, re, time, base64import sys,urllib2cityList_main = [ #全國主要城市 #華北 {'code':"101010100", 'name':"北京"}, {'code':"101030100", 'name':"天津"}, {'code':"101090101", 'name':"石家莊"}, {'code':"101100101", 'name':"太原"}, {'code':"101080101", 'name':"呼和浩特"}, {'code':"101090201", 'name':"保定"}, {'code':"101100201", 'name':"大同"}, {'code':"101080201", 'name':"包頭"}, {'code':"101090402", 'name':"承德市"}, {'code':"101100401", 'name':"晉中"}, {'code':"101080501", 'name':"通遼"}, {'code':"101091101", 'name':"秦皇島"}, #東北 {'code':"101050101", 'name':"哈爾濱"}, {'code':"101060101", 'name':"長春"}, {'code':"101070101", 'name':"瀋陽"}, {'code':"101050201", 'name':"齊齊哈爾"}, {'code':"101060201", 'name':"吉林"}, {'code':"101070201", 'name':"大連"}, {'code':"101050301", 'name':"牡丹江"}, {'code':"101060301", 'name':"延吉"}, {'code':"101070301", 'name':"鞍山"}, {'code':"101050501", 'name':"綏化"}, {'code':"101060601", 'name':"白城"}, {'code':"101071401", 'name':"葫蘆島"}, #華南 {'code':"101280101", 'name':"廣州"}, {'code':"101300101", 'name':"南寧"}, {'code':"101310101", 'name':"海口"}, {'code':"101320101", 'name':"香港"}, {'code':"101330101", 'name':"澳門"}, {'code':"101280601", 'name':"深圳"}, {'code':"101300501", 'name':"桂林"}, {'code':"101310201", 'name':"三亞"}, {'code':"101280701", 'name':"珠海"}, {'code':"101281701", 'name':"中山"}, {'code':"101301001", 'name':"百色"}, {'code':"101310215", 'name':"萬寧"}, #西北 {'code':"101110101", 'name':"西安"}, {'code':"101160101", 'name':"蘭州"}, {'code':"101150101", 'name':"西寧"}, {'code':"101170101", 'name':"銀川"}, {'code':"101130101", 'name':"烏魯木齊"}, {'code':"101110300", 'name':"延安"}, {'code':"101110901", 'name':"寶雞"}, {'code':"101160901", 'name':"天水"}, {'code':"101170301", 'name':"吳忠"}, {'code':"101130501", 'name':"吐魯番"}, {'code':"101160801", 'name':"酒泉"}, {'code':"101170401", 'name':"固原"}, #西南 {'code':"101040100", 'name':"重慶"}, {'code':"101270101", 'name':"成都"}, {'code':"101260101", 'name':"貴陽"}, {'code':"101290101", 'name':"昆明"}, {'code':"101140101", 'name':"拉薩"}, {'code':"101270401", 'name':"綿陽"}, {'code':"101260201", 'name':"遵義"}, {'code':"101290201", 'name':"大理"}, {'code':"101271401", 'name':"樂山"}, {'code':"101260801", 'name':"六盤水"}, {'code':"101291401", 'name':"麗江"}, #華東 {'code':"101020100", 'name':"上海"}, {'code':"101230101", 'name':"福州"}, {'code':"101220101", 'name':"合肥"}, {'code':"101240101", 'name':"南昌"}, {'code':"101120101", 'name':"濟南"}, {'code':"101210301", 'name':"嘉興"}, {'code':"101190101", 'name':"南京"}, {'code':"101210401", 'name':"寧波"}, {'code':"101210101", 'name':"杭州"}, {'code':"101190401", 'name':"蘇州"}, {'code':"101120201", 'name':"青島"}, {'code':"101230201", 'name':"廈門"}, {'code':"101340101", 'name':"台北市"}, #華中 {'code':"101180101", 'name':"鄭州"}, {'code':"101200101", 'name':"武漢"}, {'code':"101250101", 'name':"長沙"}, {'code':"101180201", 'name':"安陽"}, {'code':"101200201", 'name':"襄陽"}, {'code':"101250201", 'name':"湘潭"}, {'code':"101250301", 'name':"株洲"}, {'code':"101180401", 'name':"許昌"}, {'code':"101250601", 'name':"常德"}, {'code':"101251101", 'name':"張家界"}, {'code':"101200401", 'name':"孝感"}, {'code':"101201401", 'name':"荊門"}]'''{"weatherinfo":{"city":"北京","cityid":"101010100","temp":"25","WD":"東風","WS":"2級","SD":"50%","WSE":"2","time":"20:30","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB"}}'''#返回dict類型: 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']=weatherInfomation.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] except (SyntaxError) as err: print(">>>>>> SyntaxError: " + err.args) except: print(">>>>>> OtherError: ") else: return msg finally: None def getCityCodeFromName(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): #將 “浙江省杭州市” 轉換成“杭州” #print 'convertName:',cityName name = unicode(cityName, "gbk") if name.find(u"省") !=-1:# 包含'省' #print u'有省' name=name.split(u'省')[1] if name.find(u"市") != -1:#包含‘市’ #print u'有市' name=name.split(u'市')[0] return name
代碼中的城市編碼,直接使用的 http://www.oschina.net/code/snippet_734886_13619
5. 運行效果
轉載本文請註明作者和出處
作者 :JarvisChu
出處:http://blog.csdn.net/jarvischu