第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > python3.7通过itchat方式登录微信给好友发送天气信息

python3.7通过itchat方式登录微信给好友发送天气信息

时间:2021-11-27 15:34:43

相关推荐

python3.7通过itchat方式登录微信给好友发送天气信息

环境:win7,python3.7

安装好 ichat,requests,yaml,Schedule(定时功能使用)

准备一份全国地区的code;参照图一:citys.yml

需先了解全国天气相关接口API:

思路:想通过输入一个城市名就可以打印出相关天气信息;

由于yml配置文件只有城市名:城市代码code

通过城市名去获取code,在把code 引用到URL路径上进行对每个城市的天气信息进行爬取

#!/usr/bin/env python# -*- coding: utf-8 -*-from threading import Timerfrom itchat.content import *import requestsimport itchatimport yaml#调用城市名,返回对应的城市code,通过yaml模块对yml配置文件进行解析def getCityArea(cityName):with open("citys.yml",'r',encoding = "utf-8") as f:city_cfg = f.read() #返回的是一大串字符串city_dic = yaml.load(city_cfg) #需要再次转换成字典格式#print(city_cfg)#print(city_dic)f.close() # 关闭文件操作#获取城市对应的CodeIDcity_ID = city_dic['city'][cityName]#print(city_ID)return city_ID #返回ID#获取每日更新天气def getWeather(cityID):#打开全国天气APIurl = r"http://t./api/weather/city/"+str(cityID)req = requests.get(url)cityName = req.json()['cityInfo']['city']print(cityName)time = req.json()['time']print(time)shidu = req.json()['data']['shidu']pm25 = req.json()['data']['pm25']pm10 = req.json()['data']['pm10']quality = req.json()['data']['quality']wendu = req.json()['data']['wendu']ganmao = req.json()['data']['ganmao']#print("%s,%s,%s,%s,%s,%s" %(shidu,quality,wendu,ganmao,pm10,pm25))#content_Msg = cityName+"\n"+time+"\n"+"今日温度:"+wendu+"\n" +"湿度:"+shidu+"\n空气质量:"+quality+"\n"+ganmao#print(content_Msg)return cityName,time,wendu,shidu,quality,ganmao#给微信好友发送天气消息def sendWeateherInfo(City_ID):try:#会弹出网页二维码扫描登录微信itchat.auto_login() #不想每次都扫描,登录时预配置#itchat.auto_login(hotReload=True)#itchat.run()#1.想给谁发信息,先找到该朋友,备注名my_friend = itchat.search_friends(name = r'小夏')#2.找到UserNamemy_love = my_friend[0]["UserName"]#CityID = getCityArea(Name)#内容通过参数传递方法一:cityName,time,wendu,shidu,quality,ganmao = getWeather(City_ID)content_Msg = cityName+"\n"+time+"\n"+"今日温度:"+wendu+" \n " +"湿度:"+shidu+"\n空气质量:"+quality+"\n"+ganmaoprint(content_Msg)#内容直接通过函数返回 方法二:# Name = str(getWeather()[0]) #获取天气信息内容# time = str(getWeather()[1])# wendu = str(getWeather()[2])# shidu = str(getWeather()[3])# quality = str(getWeather()[4])# ganmao = str(getWeather()[5])msg1 = "\n 小夏,天气很冷 注意多穿几件衣服 保暖啊!!!"msg2 = "\n 小夏,天气温暖 多晒晒太阳哟,嘿嘿!!!"msg3 = "\n 小夏,天气太热了 注意要躲在家里。防止中暑!!!"msg4 = "\n 小夏,天气适中 可以多运动运动,去附近公园或景区散步爬山都可以的哦!!!"#注意这里返回的温度判断是一个str type,需要转换成int type# print("*****%s****"%wendu) # str 9# print(type(wendu)) #strt = int(wendu) # print(type(t)) #int# print(t) # int 9if 30 < t <= 50:#3.发送消息itchat.send(content_Msg+msg3,toUserName = "filehelper")#发送给文件助手进行调试itchat.send(content_Msg+msg3,toUserName = my_love)elif 20 < t <= 30:itchat.send(content_Msg=msg2,toUserName = "filehelper") itchat.send(content_Msg+msg2,toUserName = my_love)elif 10 < t <= 20:itchat.send(content_Msg+msg4,toUserName = "filehelper")itchat.send(content_Msg+msg4,toUserName = my_love)elif 0 < t <= 10:itchat.send(content_Msg+msg1,toUserName = "filehelper")itchat.send(content_Msg+msg1,toUserName = my_love)else:print("要么超级热要么零下几度")#每隔86400秒发送一次,每天发送一次Timer(80000,sendNews).start()except:msg5 = "发送失败,继续努力!!!"itchat.send(msg5,toUserName = my_love)#itchat.logout() 退出微信def test():itchat.auto_login()itchat.send(u'测试消息发送','filehelper')#发送给文件助手if __name__ == '__main__':Name = "深圳" #在这里城市名可以随便更换City_ID = getCityArea(Name)#getWeather(City_ID) 调试测试是否能获取到天气相关信息sendWeateherInfo(City_ID)#test()"""#定时操作方法,需要导入Schedule包sched = BlockingScheduler()sched.add_job(send_message,'cron',month='1-12',day='1-31',hour=15,minute =49)#设定发送的时间sched.start()"""

图1:yml配置文件

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。