调用和风天气和百度语音合成接口,实现在浏览器中播报实时气象情况

来源:互联网 发布:网络是市场细分对 编辑:程序博客网 时间:2024/05/21 10:08
# -*- coding=utf-8 -*-import urllib.requestimport jsonimport sysimport webbrowser#reload(sys) # Python2.5 初始化后会删除 sys.setdefaultencoding 这个方法,我们需要重新载入sys.setdefaultencoding('utf-8') #这个是解决合成中文文本的时候,Unicode和utf-8编码问题的,可以尝试注释掉会不会报错#调用和风天气的APIurl = 'https://api.heweather.com/x3/weather?cityid=CN101210101&key=d21c68c55a8243ec99c1ca7f09cb3723'#用urllib2创建一个请求并得到返回结果req = urllib.request.Request(url)resp = urllib.request.urlopen(req).read()print (resp)print ('********************************************')#将JSON转化为Python的数据结构transform_json_data = json.loads(resp)print (transform_json_data)data = transform_json_data['HeWeather data service 3.0'][0]print ('********************************************')#此时已经将天气数据拿到,并且反序列化为Python对象了,只有提取我们需要的数据即可#我就每天早上听一下今天的气温、风力、pm2.5、穿衣建议就好了。现在以获得pm2.5的值为例,#一层层获取到pm2.5的值通过postman查看json结构,JSON里面{}对应的是Python的dict,[]对应的是Python里的list,#上图中可以比较清楚地知道这个JSON得到的Python数据其实是dict和list的多层嵌套。这里我们可以类比查《新华字典》,#想要查到“天气”这个词,可以找到字母T的页码(按A~Z的有序列表),再找到“tian”所在的位置,一步步往里找就成找到想要的词了。#最外层是个dict类型:dict: {'HeWeather...": [ list]}#往里一层是多个dict,直接通过多层dict['key']['key2']...就能取得PM2.5的值了:list 只有一个值,取list[0](它的值是一个dict),#取得天气只需要:print (transform_json_data['HeWeather data service 3.0'][0]['aqi']['city']['pm25'])#后续代码#获取PM2.5的值pm25 = data['aqi']['city']['pm25']print (pm25)print ('********************************************')#获取空气质量air_quality = data['aqi']['city']['qlty']print (air_quality)print ('********************************************')#获取城市city = data['basic']['city']print (city)print ('********************************************')#获取现在的天气、温度、体感温度、风向、风力等级now_weather = data['now']['cond']['txt']now_tmp = data['now']['tmp']now_fl = data['now']['fl']now_wind_dir = data['now']['wind']['dir']now_wind_sc = data['now']['wind']['sc']print (now_weather,now_tmp,now_fl,now_wind_dir,now_wind_sc)print ('********************************************')#今天的天气today = data['daily_forecast'][0]weather_day = today['cond']['txt_d']weather_night = today['cond']['txt_n']tmp_high = today['tmp']['max']tmp_low = today['tmp']['min']wind_dir = today['wind']['dir']wind_sc = today['wind']['sc']print (weather_day,weather_night,tmp_high,tmp_low,wind_dir,wind_sc)print ('********************************************')#天气建议#舒适度comf = data['suggestion']['comf']['brf']comf_txt = data['suggestion']['comf']['txt']print (comf,comf_txt)print ('********************************************')#流感指数flu = data['suggestion']['flu']['brf']flu_txt = data['suggestion']['flu']['txt']print (flu,flu_txt)print ('********************************************')#穿衣指数drsg = data['suggestion']['drsg']['brf']drsg_txt = data['suggestion']['drsg']['txt']print (drsg,drsg_txt)print ('********************************************')#将所有信息合成一段文本weather_forcast_txt = "%s今天白天天气%s,夜间天气%s,最高气温:%s度,最低气温:%s度,风力:%s级,风向:%s方向,天气舒适度:%s,%s流感"\"指数:%s,%s 穿衣指数:%s,%s 现在外面的天气:%s,当前温度:%s度,当前风力:%s级"%(city,weather_day,weather_night,tmp_high,tmp_low,wind_sc,wind_dir,comf,comf_txt,flu,flu_txt,drsg,drsg_txt,now_weather,now_tmp,now_wind_sc)print (weather_forcast_txt)#调用百度语音合成weather_forcast_txt文本url_txt = "http://tsn.baidu.com/text2audio?tex="+weather_forcast_txt+"&lan=zh&cuid=B8-76-3F-63-3C-0C&ctp=1&tok=25.e2bca9fffd0d73b2e3a63377283aaf0b.315360000.1809931843.282335-9638256"#用浏览器播报语音webbrowser.open(url_txt)
参考资料:百度语音合成API操作文档:http://yuyin.baidu.com/docs/tts/135#获取 Access Token
          如何在python中调用浏览器并打开一个网址:http://www.jb51.net/article/50673.htm
          参考文章:https://mp.weixin.qq.com/s?__biz=MzA3MDg0MjgxNQ==&mid=2652391071&idx=1&sn=8986ead0af2111c09f7eadb5922e5af2&chksm=84da464fb3adcf596cbfb80a7208aa0229289352c4fbe9eb34f247b224410fe3a2706e330fee&mpshare=1&scene=1&srcid=0506jFSehhmrzQYeNKgmG2xY&key=6f455d10b4b4cb14cec2f0ec9f9ade2df38c5c0cf7c43561567ecaee16d10dc24b7cb9577b5d00a8faf1aabfebe6cac197863c7badd8089c029bb7c972240fd63ea41daba51b837be908504f4a80755d&ascene=1&uin=MTI2MTMzODYwNQ%3D%3D&devicetype=Windows-QQBrowser&version=61030006&pass_ticket=hQ8ow92RFHp8PNqkvsiv%2FrEtGgGti7N%2F0NZKn9MbQfenYRa%2Fl2DL3s%2FL05e%2FN%2FXe
          

0 0
原创粉丝点击