#!/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. 【39.66%】【codeforces 740C】Alyona and mex

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. [Docker] Run Short-Lived Docker Containers

    Learn the benefits of running one-off, short-lived Docker containers. Short-Lived containers are use ...

  3. Apache+tomcat的整合 分类: C_OHTERS 2014-05-07 15:08 293人阅读 评论(0) 收藏

    http://blog.csdn.net/stefyue/article/details/6918542 为什么要做这个整合呢?当然,首先想到是就是Apache和Tomcat的区别.正因为有区别,有各 ...

  4. EChats+Ajax之柱状图的数据交互

    原文链接:https://blog.csdn.net/qq_37936542/article/details/79723710 一:下载 echarts.min.js 选择完整版进行下载,精简版和常用 ...

  5. C++实现简单的内存块自己主动管理

    #ifndef __MEM__H #define __MEM__H #include<iostream> using namespace std; //自己主动管理内存块 typedef ...

  6. SpringMVC中跳转路径的问题

    1. @RequestMapping 1)@RequestMapping既可以作用于类,也可以作用于方法 2)@RequestMapping中value值(即跳转的路径),可以加 "/&qu ...

  7. [Angular] Create dynamic content with <tempalte>

    To create a dynamic template, we need to a entry component as a placeholder. Then we can use entry c ...

  8. Android自定义控件View(三)组合控件

    不少人应该见过小米手机系统音量控制UI,一个圆形带动画效果的音量加减UI,效果很好看.它是怎么实现的呢?这篇博客来揭开它的神秘面纱.先上效果图 相信很多人都知道Android自定义控件的三种方式,An ...

  9. [Redux] Important things in Redux

    Root Smart component can be overloaded, divide 'smart' component wisely & using Provider. Proble ...

  10. java-线程-ABCABC

    public class OneByOne { private Lock lock = new ReentrantLock(); private Condition conditionA = lock ...