python运行py文件 mac_MAC定时任务-执行python文件

#!/usr/bin/env python

import requests

import json

import schedule

import time

import json, urllib

from urllib import request

def requestw():

now = 'https://free-api.heweather.net/s6/weather/now?location=hangzhou&key=**'

forecast = 'https://free-api.heweather.net/s6/weather/forecast?location=hangzhou&key=**'

lifestyle = 'https://free-api.heweather.net/s6/weather/lifestyle?location=hangzhou&key=**'

request_now = request.Request(now)

result_now = urllib.request.urlopen(request_now)

jsonarr_now = json.loads(result_now.read())

#fl:体感温度,cond_txt:实况天气状况描述,wind_sc:风力

fl= jsonarr_now['HeWeather6'][0]['now']['fl']

tmp=jsonarr_now['HeWeather6'][0]['now']['tmp']

cond_txt= jsonarr_now['HeWeather6'][0]['now']['cond_txt']

wind_sc= jsonarr_now['HeWeather6'][0]['now']['wind_sc']

request_forecast = request.Request(forecast)

result_forecast = urllib.request.urlopen(request_forecast)

jsonarr_forecast = json.loads(result_forecast.read())

#cond_txt_d_1:明天实况天气状况描述,tmp_max_1:最高温度,

cond_txt_d_1= jsonarr_forecast['HeWeather6'][0]['daily_forecast'][1]['cond_txt_d']

tmp_max_1= jsonarr_forecast['HeWeather6'][0]['daily_forecast'][1]['tmp_max']

tmp_min_1= jsonarr_forecast['HeWeather6'][0]['daily_forecast'][1]['tmp_min']

cond_txt_d_2= jsonarr_forecast['HeWeather6'][0]['daily_forecast'][2]['cond_txt_d']

tmp_max_2= jsonarr_forecast['HeWeather6'][0]['daily_forecast'][2]['tmp_max']

tmp_min_2= jsonarr_forecast['HeWeather6'][0]['daily_forecast'][2]['tmp_min']

request_lifestyle = request.Request(lifestyle)

result_lifestyle = urllib.request.urlopen(request_lifestyle)

jsonarr_lifestyle = json.loads(result_lifestyle.read())

#comf:舒适度指数,drsg:穿衣指数

comf= jsonarr_lifestyle['HeWeather6'][0]['lifestyle'][0]['txt']

drsg= jsonarr_lifestyle['HeWeather6'][0]['lifestyle'][1]['txt']

url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=**'

headers = {'content-type': "application/json", 'Authorization': 'APP appid = **,token = **'}

body = {

"msgtype": "markdown",

"markdown": {

"content": "**今日天气状况:** "+ cond_txt +" \n"

">实时温度: "+ tmp + "摄氏度\n"

">体感温度: "+ fl + "摄氏度\n"

">风力: "+ wind_sc + "\n"

">舒适度指数: "+ comf + "\n"

">穿衣指数: "+ drsg + "\n\n"

"**明日天气状况:** "+ cond_txt_d_1 +" \n"

">明日最高温度: "+ tmp_max_1 + "摄氏度\n"

">明日最低温度: "+ tmp_min_1 + "摄氏度\n\n"

"**后日天气状况:** "+ cond_txt_d_2 +" \n"

">后日最高温度: "+ tmp_max_2 + "摄氏度\n"

">后日最低温度: "+ tmp_min_2 + "摄氏度\n"

}

}

response = requests.post(url, data = json.dumps(body), headers = headers)

schedule.every(30).minutes.do(requestw)

while True:

schedule.run_pending()

time.sleep(1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值