python3通过json获取天气信息

           通过python,每天定时发送今明俩天的天气信息

           测试的时候可以发送给自己,后面可以发送给指定的人或群发

from __future__ import unicode_literals
from threading import Timer
from wxpy import *
import requests
import urllib.request
import json
	
def get_weather(city_code):
	url = "http://t.weather.sojson.com/api/weather/city/" + city_code
	#print(url)
	content = urllib.request.urlopen(url).read()
	all_data =json.loads(content)
	
	today = all_data['date']
	#print(today)

	city_info = all_data['cityInfo']
	city = city_info['city']
	#print(city)
		
	data = all_data['data']
	
	temperature = data['wendu']
	#print(data['wendu'])
	
	humidity = data['shidu']
	#print(humidity)
	
	air_quality = data['quality']
	
	#print(air_quality)
	
	forecast_info = data['forecast']
	#print(forecast_info)
	tomorrow_info = forecast_info[1]
	#print(tomorrow_info)
	
	tomorrow_date = tomorrow_info['date']
	#print(tomorrow_date)
	
	tomorrow_sunrise = tomorrow_info['sunrise']
	#print(tomorrow_sunrise)
	
	tomorrow_sunset = tomorrow_info['sunset']
	#print(tomorrow_sunset)
	tomorrow_date = tomorrow_info['date']
	#print(tomorrow_date)
	
	tomorrow_low = tomorrow_info['low']
	#print(tomorrow_low)
	tomorrow_high = tomorrow_info['high']
	#print(tomorrow_high)
	
	return ("日期:{0}\n城市:{1}\n温度:{2}\n湿度:{3}\n 空气质量:{4}\n\
			明天:{5}\n明天气温:{6}~{7}\n明天日出:{8}\n明天日落:{9}"\
			.format(today, city,temperature,humidity,air_quality,\
			tomorrow_date,tomorrow_low,tomorrow_high,tomorrow_sunrise,tomorrow_sunset))

def send_news():
	'''城市码可以在网上查询'''
	codes = ["101280301","101280303","101280304","101280401","101280601"]
	data = get_weather(codes[0])
	bot = Bot(cache_path=True)
	'''这是是发送给文件助手,也可以发送给指定的朋友或群发'''
	myself = bot.self
	bot.file_helper.send(data)
	
	'''一天86400秒,测试的时候可以改短'''
	t = Timer(86400, send_news) 
	t.start()

if __name__ == "__main__":
	send_news()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值