#!/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获取天气预报的更多相关文章

  1. [整]C#获取天气预报信息(baidu api)包括pm2.5

    /// <summary> /// 获取天气预报信息 /// </summary> /// <returns></returns> public Bai ...

  2. java获取天气预报的信息

    运行效果: 主要功能: 1,jsp页面输入省份和城市 根据条件获取当地的天气信息 2,java代码 利用第三方的省份和城市的路径地址 本工程主要实现java获取天气预报的信息步骤1,创建工程weath ...

  3. python3 获取cookie解决方案

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

  4. Python3 获取网络图片并且保存到本地

    Python3 获取网络图片并且保存到本地 import requests from bs4 import BeautifulSoup from urllib import request impor ...

  5. python3获取网页天气预报信息并打印

    查到一个可以提供区域天气预报的url, https://www.sojson.com/open/api/weather/json.shtml?city=%E6%88%90%E9%83%BD打算用pyt ...

  6. Android 获取天气预报

    界面布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...

  7. python3 获取阿里云ECS 实例及监控的方法

    #!/usr/bin/env python3.5 # -*- coding:utf8 -*- try: import httplib except ImportError: import http.c ...

  8. php从气象局获取天气预报并保存到服务器

    思路:1.打开网页时读取中国气象网的接口得到每个城市的该日json:2.解析并保存到mysql:3.客户端访问mysql得到数据集. 所包含的技巧: 进度条.flush()问题.mysql.xml.p ...

  9. 获取天气预报API5_统计最容易生病时间段

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

随机推荐

  1. 4、linux下应用创建线程

    1.linux创建线程之pthread_create 函数简介 pthread_create是UNIX环境创建线程函数 头文件 #include<pthread.h> 函数声明 int p ...

  2. GCD 初步学习

    GCD应该是比較牛逼的东西了吧,一时半会应该是操作不好. 在cocoa-china上面有两篇关于GCD的文章.GCD 深入理解(一) GCD 深入理解(二) CSDN荣芳志博客:点击打开链接 我仅仅是 ...

  3. [AngularJS] Directive for top scroll bar

    angular.directive('dblScroll', dblScroll) dblSroll.$inject = [ '$timeout' ]; function dblScroll($tim ...

  4. iOS View自定义窍门——UIButton实现上显示图片,下显示文字

    “UIButton实现上显示图片,下显示文字”这个需求相信大家在开发中都或多或少会遇见.比如自定义分享View的时候.当然,也可以封装一个item,上边imageView,下边一个label.但是既然 ...

  5. jquery中的this与$(this)的区别总结(this:html元素)($(this):JQuery对象)

    jquery中的this与$(this)的区别总结(this:html元素)($(this):JQuery对象) 一.总结 1.this所指的是html 元素,有html的属性,可用 this.属性  ...

  6. 深度学习代码注解(一)—— mnistdeepauto

    clear all close all %% 1:参数设置 maxepoch=10; %In the Science paper we use maxepoch=50, but it works ju ...

  7. 标准模板库(STL) map —— 初始化问题

    map 容器没有:.reverse成员: map 是关联式容器,会根据元素的键值自动排序: map 容器不是连续的线性空间: 标准 STL 使用 RB-tree 为底层机制 ⇒ 自动排序(关于键值): ...

  8. JS中的JSON对象 定义和取值

    1.JSON(JavaScript Object Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScript原生格式,这意味着在JavaScript中处理JSON数据不需要任 ...

  9. [Grid Layout] Use auto-fill and auto-fit if the number of repeated grid tracks is not to be def

    What about the situation in which we aren’t specifying the number of columns or rows to be repeated? ...

  10. RAC sysdate

    rac的sysdate可能不一致,这个需要注意.