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 ...
随机推荐
- 【Codeforces Round #185 (Div. 2) B】Archer
[链接] 链接 [题意] 在这里输入题意 [题解] 概率水题. 枚举它是第几轮成功的. 直到满足精度就好 [错的次数] 1 [反思] long double最让人安心. [代码] #include & ...
- POJ 1258 Agri-Net|| POJ 2485 Highways MST
POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...
- (十二)RabbitMQ消息队列-性能测试
原文:(十二)RabbitMQ消息队列-性能测试 硬件配置 宿主机用的联想3850X6的服务器四颗E7-4850v3的处理器,DDR4内存,两块1.25TB的pcie固态.在宿主机上使用的事esxi5 ...
- spring里头各种获取ApplicationContext的方法
为啥写这个文章呢?spring各个版本不同,以及和系统框架套在一起不同,导致获取的方式不同,网络上各种版本,太乱了,写获取方式的人都不写这个获取方式是在本地还是在WEB,在那种应用服务器下,在spri ...
- [Webpack] Configure Prepack with Webpack
Great improvements and optimizations can be made to the output of bundled code. Prepack provides the ...
- java生成UUID通用唯一识别码 (Universally Unique Identifier) 分类: B1_JAVA 2014-08-22 16:09 331人阅读 评论(0) 收藏
转自:http://blog.csdn.net/carefree31441/article/details/3998553 UUID含义是通用唯一识别码 (Universally Unique Ide ...
- Selector API用法
java.nio.channels 类 Selector java.lang.Object java.nio.channels.Selector 直接已知子类: AbstractSelector pu ...
- Linux环境下Apache ActiveMQ 基本安装
原文链接:https://www.jianshu.com/p/1c017088aa95 在linux上安装mq,并映射到外网.1.Apache ActiveMQ安装基本条件请参考链接:2.下载Apac ...
- Android使用JNI实现Java与C之间传递数据
介绍Java如何将数据传递给C和C回调Java的方法. java传递数据给C,在C代码中进行处理数据,处理完数据后返回给java.C的回调是Java传递数据给C,C需要用到Java中的某个方法,就需要 ...
- Android的NDK开发(5)————Android JNI层实现文件的read、write与seek操作
1. 在Android的Java层实现文件的读写操作是非常简单的,可以参看之前写的博文:http://blog.csdn.net/conowen/article/details/7296121 在JN ...