import requests import json import time # address = input() # 输入要查询的地址 address = ‘湘潭市湖南科技大学‘ # 获取输入地址的经纬度 map_key = ‘腾讯地图API key‘ map_url = ‘https://apis.map.qq.com/ws/geocoder/v1/?address=‘ + address + ‘&key=‘ + map_key map_response = requests.get(map_url) map_json = json.loads(map_response.text) latitude = str(map_json[‘result‘][‘location‘][‘lat‘]) longitude = str(map_json[‘result‘][‘location‘][‘lng‘]) # print(address + longitude + latitude) # 根据经纬度查询天气 api_key = ‘彩云天气API key‘ # api秘钥 json_url = ‘https://api.caiyunapp.com/v2/‘ + api_key + ‘/‘ + longitude + ‘,‘ + latitude + ‘/realtime.json‘ json_response = requests.get(json_url) json_str = json.loads(json_response.text) timeStamp = json_str[‘server_time‘] localTime = time.localtime(timeStamp) formatTime = time.strftime("%Y{y}%m{m}%d{d} %H{c}%M{mi}%S{s}").format(y=‘年‘, m=‘月‘, d=‘日‘, c=‘点‘, mi=‘分‘, s=‘秒‘) temperature = str(json_str[‘result‘][‘temperature‘]) # 气温 humidity = json_str[‘result‘][‘humidity‘] # 湿度 skyconndition = str(json_str[‘result‘][‘skycon‘]) # 天气状况 weatherdic = {‘CLEAR_DAY‘: ‘晴‘, ‘CLEAR_NIGHT‘: ‘晴‘, ‘PARTLY_CLOUDY_DAY‘: ‘多云‘, ‘PARTLY_CLOUDY_NIGHT‘: ‘多云‘, ‘CLOUDY‘: ‘阴‘, ‘WIND‘: ‘阴‘, ‘HAZE‘: ‘雾霾‘, ‘RAIN‘: ‘雨‘, ‘SNOW‘: ‘雪‘} skyconndition = weatherdic[skyconndition] # 匹配代码与文字 aqi = str(json_str[‘result‘][‘aqi‘]) # 空气质量指数 if int(aqi) >= 200: aqi = ‘200 \n雾霾天要少出去玩哦‘ # pm25 = str(json_str[‘result‘][‘pm25‘]) real_humidity = str(humidity * 100) current_weather = address + ‘\n北纬‘ + str(latitude) + ‘ 东经‘ + str( longitude) + ‘\n实时天气:\n气温:‘ + temperature + ‘℃\n湿度:‘ + real_humidity + ‘%\n天气:‘ + skyconndition + ‘\nAQI:‘ + aqi + ‘\n时间:‘ + formatTime print(current_weather) def weather(): headers = {"Content-Type": "text/plain"} # 苏宁接口获取时间 # timeUrl = ‘http://quan.suning.com/getSysTime.do‘ # timeResponse = requests.get(timeUrl) # timeJson = json.loads(timeResponse.text) # severTime = timeJson[‘sysTime2‘] # 2020-02-21 12:26:12 s = current_weather data = { "msgtype": "text", "text": { "content": s, } } r = requests.post( url=‘http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=企业微信机器人key‘, # webhook地址 headers=headers, json=data) print(r.text) weather()
原文地址:https://www.cnblogs.com/loneykids/p/12343658.html
时间: 2024-10-16 22:40:41