python3获取天气预报
#!/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':"", 'name':"北京"},
{'code':"", 'name':"上海"},
{'code':"", 'name':"广州"},
{'code':"", 'name':"深圳"}
] cityList_main = [ #全国主要城市
#华北
{'code':"", 'name':"北京"},
{'code':"", 'name':"天津"},
{'code':"", 'name':"石家庄"},
{'code':"", 'name':"太原"},
{'code':"", 'name':"呼和浩特"},
{'code':"", 'name':"保定"},
{'code':"", 'name':"大同"},
{'code':"", 'name':"包头"},
{'code':"", 'name':"承德市"},
{'code':"", 'name':"晋中"},
{'code':"", 'name':"通辽"},
{'code':"", 'name':"秦皇岛"},
#东北
{'code':"", 'name':"哈尔滨"},
{'code':"", 'name':"长春"},
{'code':"", 'name':"沈阳"},
{'code':"", 'name':"齐齐哈尔"},
{'code':"", 'name':"吉林"},
{'code':"", 'name':"大连"},
{'code':"", 'name':"牡丹江"},
{'code':"", 'name':"延吉"},
{'code':"", 'name':"鞍山"},
{'code':"", 'name':"绥化"},
{'code':"", 'name':"白城"},
{'code':"", 'name':"葫芦岛"},
#华南
{'code':"", 'name':"广州"},
{'code':"", 'name':"南宁"},
{'code':"", 'name':"海口"},
{'code':"", 'name':"香港"},
{'code':"", 'name':"澳门"},
{'code':"", 'name':"深圳"},
{'code':"", 'name':"桂林"},
{'code':"", 'name':"三亚"},
{'code':"", 'name':"珠海"},
{'code':"", 'name':"中山"},
{'code':"", 'name':"百色"},
{'code':"", 'name':"万宁"},
#西北
{'code':"", 'name':"西安"},
{'code':"", 'name':"兰州"},
{'code':"", 'name':"西宁"},
{'code':"", 'name':"银川"},
{'code':"", 'name':"乌鲁木齐"},
{'code':"", 'name':"延安"},
{'code':"", 'name':"宝鸡"},
{'code':"", 'name':"天水"},
{'code':"", 'name':"吴忠"},
{'code':"", 'name':"吐鲁番"},
{'code':"", 'name':"酒泉"},
{'code':"", 'name':"固原"},
#西南
{'code':"", 'name':"重庆"},
{'code':"", 'name':"成都"},
{'code':"", 'name':"贵阳"},
{'code':"", 'name':"昆明"},
{'code':"", 'name':"拉萨"},
{'code':"", 'name':"绵阳"},
{'code':"", 'name':"遵义"},
{'code':"", 'name':"大理"},
{'code':"", 'name':"乐山"},
{'code':"", 'name':"六盘水"},
{'code':"", 'name':"丽江"},
#华东
{'code':"", 'name':"上海"},
{'code':"", 'name':"福州"},
{'code':"", 'name':"合肥"},
{'code':"", 'name':"南昌"},
{'code':"", 'name':"济南"},
{'code':"", 'name':"嘉兴"},
{'code':"", 'name':"南京"},
{'code':"", 'name':"宁波"},
{'code':"", 'name':"杭州"},
{'code':"", 'name':"苏州"},
{'code':"", 'name':"青岛"},
{'code':"", 'name':"厦门"},
{'code':"", 'name':"台北市"},
#华中
{'code':"", 'name':"郑州"},
{'code':"", 'name':"武汉"},
{'code':"", 'name':"长沙"},
{'code':"", 'name':"安阳"},
{'code':"", 'name':"襄阳"},
{'code':"", 'name':"湘潭"},
{'code':"", 'name':"株洲"},
{'code':"", 'name':"许昌"},
{'code':"", 'name':"常德"},
{'code':"", 'name':"张家界"},
{'code':"", 'name':"孝感"},
{'code':"", '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" + "" + 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获取天气预报的更多相关文章
- [整]C#获取天气预报信息(baidu api)包括pm2.5
/// <summary> /// 获取天气预报信息 /// </summary> /// <returns></returns> public Bai ...
- java获取天气预报的信息
运行效果: 主要功能: 1,jsp页面输入省份和城市 根据条件获取当地的天气信息 2,java代码 利用第三方的省份和城市的路径地址 本工程主要实现java获取天气预报的信息步骤1,创建工程weath ...
- python3 获取cookie解决方案
python3 获取cookie解决方案 方案一: 利用selenium+phantomjs无界面浏览器的形式访问网站,再获取cookie值: from selenium import webdriv ...
- Python3 获取网络图片并且保存到本地
Python3 获取网络图片并且保存到本地 import requests from bs4 import BeautifulSoup from urllib import request impor ...
- python3获取网页天气预报信息并打印
查到一个可以提供区域天气预报的url, https://www.sojson.com/open/api/weather/json.shtml?city=%E6%88%90%E9%83%BD打算用pyt ...
- Android 获取天气预报
界面布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...
- python3 获取阿里云ECS 实例及监控的方法
#!/usr/bin/env python3.5 # -*- coding:utf8 -*- try: import httplib except ImportError: import http.c ...
- php从气象局获取天气预报并保存到服务器
思路:1.打开网页时读取中国气象网的接口得到每个城市的该日json:2.解析并保存到mysql:3.客户端访问mysql得到数据集. 所包含的技巧: 进度条.flush()问题.mysql.xml.p ...
- 获取天气预报API5_统计最容易生病时间段
sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...
随机推荐
- php面试题5
php面试题5 一.总结 二.php面试题5 1. 什么事面向对象?主要特征是什么?1) 面向对象是程序的一种设计方式,它利于提高程序的重用性,是程序结构更加清晰.2) 主要特征:封装.继承.多态 2 ...
- NSCache使用常见错误
NSCache用来存储缓存数据的时候.和NSDictionary功能类似, 可是NSCache有一个特别的问题: 一旦接收到内存警告之后,假设使用[NSCache removeAllObjects]处 ...
- window.onload,<body onload="function()">, document.onreadystatechange, httpRequest.onreadystatechang 分类: C1_HTML/JS/JQUERY 2014-08-06 16:47 558人阅读 评论(0) 收藏
部分内容参考:http://www.aspbc.com/tech/showtech.asp?id=1256 在开发的过程中,经常使用window.onload和body onload两种,很少使用do ...
- javaScript基本事件
点击事件: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...
- 《Kinect应用开发实战》读书笔记---干货集合
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/45029841 作者:ca ...
- 【u005】封锁阳光大学
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街.河蟹看到欢快的曹,感到不爽.河蟹 ...
- [tmux] Organize your terminal using tmux panes
Learn to organize your workspace using tmux. We'll create a new tmux session and learn how to create ...
- html5-4 HTML5超链接、URL地址和表格
html5-4 HTML5超链接.URL地址和表格 一.总结 一句话总结: 1.cellspace有什么用? 清除表格的单元格间距 26 <table border='1px' cellspac ...
- C++ 指向类成员函数指针的用法(转自维基百科)
类成员函数指针 类成员函数指针(member function pointer),是C++语言的一类指针数据类型,用于存储一个指定类具有给定的形参列表与返回值类型的成员函数的访问信息. 目录 1 语法 ...
- 【codeforces 546E】Soldier and Traveling
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...