python自动获取天气_python 自动获取天气程序(V1)

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/jarvischu

import sys,urllib2,appGetWeather_help

#Locate the city

city_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 short

cityName=appGetWeather_help.convertName(city)

#print cityName

#convert city name to cityID

cityID=appGetWeather_help.getCityCodeFromName(cityName)

#print "cityId:",cityID

#---------------------------------------------------

#get the weather

msg = 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, base64

import sys,urllib2

cityList_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

5. 运行效果

0818b9ca8b590ca3270a3433284dd417.png

转载本文请注明作者和出处

作者 :JarvisChu

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值