python推送每日天气
import requestsimport json
spkey ='888888888888888888888888888888'
def Get_iciba_everyday():#通过爱词霸的接口获取每日的句子进行推送
iciba = "http://open.iciba.com/dsapi/"
res = requests.get(url=iciba)
res = json.loads(res.content.decode('utf-8'))
English = res['content']
Chinese = res['note']
str= '小陈的奇怪知识\n'+English+'\n'+Chinese
return str
def send(*args):
try:
api= 'http://t.weather.itboy.net/api/weather/city/+城市代码'
res = requests.get(api)
d = res.json()
if(d['status'] == 200):
parent = d['cityInfo']['parent']#省份
city = d['cityInfo']['city']#城市
update_time = d['time']#更新的时间
date = d["data"]["forecast"]["ymd"]# 日期
week = d["data"]["forecast"]["week"]# 星期
weather_type = d["data"]["forecast"]["type"]# 天气
wendu_high = d["data"]["forecast"]["high"]# 最高温度
wendu_low = d["data"]["forecast"]["low"]# 最低温度
shidu = d["data"]["shidu"]# 湿度
shidu = d["data"]["shidu"]# 湿度
pm25 = str(d["data"]["pm25"])# PM2.5
pm10 = str(d["data"]["pm10"])# PM10
quality = d["data"]["quality"]# 天气质量
fx = d["data"]["forecast"]["fx"]# 风向
fl = d["data"]["forecast"]["fl"]# 风力
ganmao = d["data"]["ganmao"]# 感冒指数
tips = d["data"]["forecast"]["notice"]# 温馨提示
cpurl = '酷推链接' + spkey#通过酷推 推送到目标群
tdwt = Get_iciba_everyday() + "\n-----------------------------------------" + "\n【今日份天气】\n城市: " + parent + city + \
"\n日期: " + date + "\n星期: " + week + "\n天气: " + weather_type + "\n温度: " + wendu_high + " / " + wendu_low + "\n湿度: " + \
shidu + "\nPM25: " + pm25 + "\nPM10: " + pm10 + "\n空气质量: " + quality + \
"\n风力风向: " + fx + fl + "\n感冒指数: " + ganmao + "\n温馨提示: " + tips + "\n更新时间: " + update_time
requests.post(cpurl, tdwt.encode('utf-8'))
except:
error = '【出现错误】\n今日天气推送错误,请联系小陈检查服务或网络状态!'
print(error)
if __name__== '__main__':
send()
页:
[1]