#!/usr/local/bin/python3
#coding=utf-8
'''
Created on 2011-2-25
@author: http://www.cnblogs.com/txw1958/
'''
#http://www.weather.com.cn/data/sk/101010100.html         实时
#http://www.weather.com.cn/data/cityinfo/101010100.html   全天
#http://m.weather.com.cn/data/101010100.html     六天
##http://www.google.com/ig/api?weather=Beijing 英文
##http://www.google.com/ig/api?weather=TOKYO
##http://www.google.com/ig/api?weather=Beijing&hl=zh-cn 中文
import os, io, sys, re, time, base64, json
import webbrowser, urllib.request
#北上广深
cityList_bsgs = [
{'code':"101010100", 'name':"北京"},
{'code':"101020100", 'name':"上海"},
{'code':"101280101", 'name':"广州"},
{'code':"101280601", 'name':"深圳"}
]
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':"荆门"}
]
#返回dict类型: twitter = {'image': imgPath, 'message': content}
def getCityWeather_RealTime(cityID):
url = "http://www.weather.com.cn/data/sk/" + str(cityID) + ".html"
try:
stdout = urllib.request.urlopen(url)
weatherInfomation = stdout.read().decode('utf-8')
jsonDatas = json.loads(weatherInfomation)
city        = jsonDatas["weatherinfo"]["city"]
temp        = jsonDatas["weatherinfo"]["temp"]
fx          = jsonDatas["weatherinfo"]["WD"]        #风向
fl          = jsonDatas["weatherinfo"]["WS"]        #风力
sd          = jsonDatas["weatherinfo"]["SD"]        #相对湿度
tm          = jsonDatas["weatherinfo"]["time"]
content = "#" + city + "#" + " " + temp + "℃ " + fx + fl + " " + "相对湿度" + sd + " "  + "发布时间:" + tm
twitter = {'image': "", 'message': content}
except (SyntaxError) as err:
print(">>>>>> SyntaxError: " + err.args)
except:
print(">>>>>> OtherError: ")
else:
return twitter
finally:
None
#返回dict类型: twitter = {'image': imgPath, 'message': content}
def getCityWeatherDetail_SixDay(cityID):
url = "http://m.weather.com.cn/data/" + str(cityID) + ".html"
try:
stdout = urllib.request.urlopen(url)
weatherInfomation = stdout.read().decode('utf-8')
jsonDatas = json.loads(weatherInfomation)
city        = jsonDatas["weatherinfo"]["city"]
tempF1      = jsonDatas["weatherinfo"]["tempF1"]
weather     = jsonDatas["weatherinfo"]["img_title1"]
img         = jsonDatas["weatherinfo"]["img1"]
fx          = jsonDatas["weatherinfo"]["fx1"]       #风向
cy            = jsonDatas["weatherinfo"]["index"]        #暖        #穿衣指数
zw            = jsonDatas["weatherinfo"]["index_uv"]        #最弱   #紫外线指数
xc            = jsonDatas["weatherinfo"]["index_xc"]        #不宜     #洗车
tr            = jsonDatas["weatherinfo"]["index_tr"]        #很适宜    #旅游
co            = jsonDatas["weatherinfo"]["index_co"]        #舒适     #舒适度
cl            = jsonDatas["weatherinfo"]["index_cl"]        #较适宜  #晨练指数
ls            = jsonDatas["weatherinfo"]["index_ls"]        #不太适宜  #晾晒指数
ag            = jsonDatas["weatherinfo"]["index_ag"]        #不易发"    #过敏
temp1       = jsonDatas["weatherinfo"]["temp1"]
temp2       = jsonDatas["weatherinfo"]["temp2"]
temp3       = jsonDatas["weatherinfo"]["temp3"]
temp4       = jsonDatas["weatherinfo"]["temp4"]
temp5       = jsonDatas["weatherinfo"]["temp5"]
temp6       = jsonDatas["weatherinfo"]["temp6"]
weather1    = jsonDatas["weatherinfo"]["weather1"]
weather2    = jsonDatas["weatherinfo"]["weather2"]
weather3    = jsonDatas["weatherinfo"]["weather3"]
weather4    = jsonDatas["weatherinfo"]["weather4"]
weather5    = jsonDatas["weatherinfo"]["weather5"]
weather6    = jsonDatas["weatherinfo"]["weather6"]
if int(img) < 10:
imgPath = "icon\d" + "0" + str(img) + ".gif"
else:
imgPath = "icon\d"       + str(img) + ".gif"
content = "#" + city + "#" + "\n<指数> " + "穿衣:" + cy + " 紫外线:" + zw + " 洗车:" + xc \
+ " 旅游:" + tr + " 舒适度:" + co + " 晨练:" + cl + " 晾晒:" + ls + " 过敏:" + ag + "\n" \
+ "<天气>" + " 1天:" + temp1 + " " + weather1 + " 2天:" + temp2 + " " + weather2  + " 3天:" + temp3 + " " + weather3\
+ " 4天:" + temp4 + " " + weather4 + " 5天:" + temp5 + " " + weather5  + " 6天:" + temp6 + " " + weather6
twitter = {'image': imgPath, 'message': content}
except (SyntaxError) as err:
print(">>>>>> SyntaxError: " + err.args)
except:
print(">>>>>> OtherError: ")
else:
return twitter
finally:
None
#返回dict类型: twitter = {'image': imgPath, 'message': content}
def getCityWeather_AllDay(cityID):
url = "http://www.weather.com.cn/data/cityinfo/" + str(cityID) + ".html"
try:
stdout = urllib.request.urlopen(url)
weatherInfomation = stdout.read().decode('utf-8')
jsonDatas = json.loads(weatherInfomation)
city        = jsonDatas["weatherinfo"]["city"]
temp1       = jsonDatas["weatherinfo"]["temp1"]
temp2       = jsonDatas["weatherinfo"]["temp2"]
weather     = jsonDatas["weatherinfo"]["weather"]
img1        = jsonDatas["weatherinfo"]["img1"]
img2        = jsonDatas["weatherinfo"]["img2"]
ptime        = jsonDatas["weatherinfo"]["ptime"]
content = city + "," + weather + ",最高气温:" + temp1 + ",最低气温:"  + temp2 + ",发布时间:" + ptime
twitter = {'image': "icon\d" + img1, 'message': content}
except (SyntaxError) as err:
print(">>>>>> SyntaxError: " + err.args)
except:
print(">>>>>> OtherError: ")
else:
return twitter
finally:
None
def main():
"main function"
for city in cityList_bsgs:
title_small = "【实时】"
twitter = getCityWeather_RealTime(city['code'])
print(title_small + twitter['message'])
for city in cityList_bsgs:
title_small = "【全天】"
twitter = getCityWeather_AllDay(city['code'])
print(title_small + twitter["message"])
title_small = "【六天】"
twitter = getCityWeatherDetail_SixDay(cityList_bsgs[3]['code'])
print(title_small + twitter["message"])
if __name__ == '__main__':
main()
*** Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32. ***
>>>
*** Remote Interpreter Reinitialized  ***
>>>
【实时】#北京# 30℃ 西南风2级 相对湿度61% 发布时间:15:20
【实时】#上海# 32℃ 东北风2级 相对湿度53% 发布时间:15:00
【实时】#广州# 35℃ 西风2级 相对湿度51% 发布时间:15:20
【实时】#深圳# 32℃ 西南风3级 相对湿度58% 发布时间:15:20
【全天】北京,多云,最高气温:31℃,最低气温:25℃,发布时间:11:00
【全天】上海,多云,最高气温:33℃,最低气温:27℃,发布时间:11:00
【全天】广州,多云,最高气温:35℃,最低气温:25℃,发布时间:11:00
【全天】深圳,多云,最高气温:33℃,最低气温:28℃,发布时间:11:00
【六天】#深圳#
<指数> 穿衣:炎热 紫外线:中等 洗车:较不宜 旅游:较适宜 舒适度:较不舒适 晨练:较适宜 晾晒:适宜 过敏:极不易发
<天气> 1天:33℃~28℃ 多云 2天:33℃~28℃ 多云 3天:34℃~28℃ 多云 4天:32℃~26℃ 雷阵雨转中雨 5天:31℃~26℃ 大雨 6天:30℃~27℃ 中雨转阵雨
>>> 

python3获取天气预报相关推荐

  1. php根据城市获取天气预报,根据浏览者ip获取城市,然后在获取城市天气预报

    /** * 根据经纬度获取城市 * @param unknown $point * @return mixed */ public function getCity() { $ip = $_SERVE ...

  2. python3 获取当前路径_如何使用python3获取当前路径及os.path.dirname的使用

    这篇文章主要介绍了如何使用python3获取当前路径及os.path.dirname的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 方法一: ...

  3. python3文件下载-python3获取文件中url内容并下载代码实例

    这篇文章主要介绍了python3获取文件中url内容并下载代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 #!/usr/bin/env py ...

  4. 通过Web Service获取天气预报并朗读

    环境:Visual Studio 2010 C# 1. 新建项目-->Windows窗体应用程序.输入解决方案名,假定为TTS. 2. 在界面设计里拉一个textBox和一个Button.tex ...

  5. webservice获取天气预报异常

    有时候需要在自己的项目中获取天气预报信息: 这个通常都是用webservice做的:不论.Net, Java:相信很多人都做过. 最常用的webservice获取天气预报的网站, http://www ...

  6. python3 获取cookie解决方案

    python3 获取cookie解决方案 方案一: 利用selenium+phantomjs无界面浏览器的形式访问网站,再获取cookie值: from selenium import webdriv ...

  7. html百度天气api,百度API 免费接口获取天气预报

    百度API 免费接口获取天气预报 发布时间:2020-08-06 05:38:12 来源:51CTO 阅读:4726 作者:大大果 Document #result{width: 800px;bord ...

  8. Postman接口测试工具学习笔记(二)Postman进阶实践:获取天气预报实例

    Postman接口测试工具学习笔记(一) 入门及基本概念 https://blog.csdn.net/sevensolo/article/details/87208546 Postman进阶实践:获取 ...

  9. html js获取天气预报,原生JS实现天气预报

    本文实例为大家分享了JS实现天气预报的具体代码,供大家参考,具体内容如下 HTML代码 content="width=device-width, user-scalable=no, init ...

  10. 爬虫获取天气预报并可视化

    重庆交通大学 期末大作业 实验课程名称:网络通信编程 学院:信息科学与工程学院 班级:物联网1901 姓名:许俊 学号:631907090127 实验类型:综合性,设计性 指导教师:娄路 开课时间:2 ...

最新文章

  1. 资料验证的asp.net程序
  2. 不得不知的小程序基本知识
  3. [iOS] 建立与使用Framework
  4. 修改oracle内存大小
  5. 【报告分享】2021年中国“夜猫”人群洞察报告:月亮不睡我不睡,我是秃头小宝贝.pdf(附下载链接)...
  6. Webpack4 学习笔记六 多页面配置和devtool
  7. 内核函数-assembler.h分析
  8. iOS 高德地图(二)(进阶具体使用的细节)
  9. 苹果你深深的伤害了我,还不让我说
  10. 大学学计算机专业要自带电脑吗,大学上课要带电脑吗
  11. Spring Boot 的配置文件有哪几种格式?它们有什么区别?
  12. WebSocket之仿QQWeb即时聊天系统(下)
  13. Ubuntu整个系统迁移到其他盘办法
  14. ttkefu微信二维码对话链接如何生成?
  15. VirtualNES虚拟红白机
  16. Windows Server 2012 R2 部署MySQL服务 安装环境错误整理
  17. python单元测试uuitest模块
  18. 百度网盘安装 不能打开要写入的文件的解决方法
  19. hive启动报错遇到的各种问题总结
  20. 怎样健康的使用计算机,S健康怎么用 三星S健使用图文教程

热门文章

  1. svchost.exe是什么进程
  2. Web页面无法执行CGI的exe程序
  3. 成长的日记教案计算机,第一单元《成长日记ABC》教学设计
  4. HP JetDirect 170X 配置
  5. cmd 删除系统垃圾文件
  6. linux autorun.sh,linux autorun使用详解
  7. [BZOJ4134][JZOJ4401]ljw和lzr的hack比赛
  8. Mysql从入门到入魔——3. 查询、排序、WHERE过滤
  9. 使用NoSQLBooster for MongoDB连接远程服务器
  10. 互联网广告的形式表现有哪几种?