主要思路
- 从wxpy获取好友列表,也可发给指定好友
- 创建定时器,也可以随时发送
- 定时器触发函数
- 函数执行,遍历好友列表
- 好友对象执行带参函数,参数为该好友城市
- 函数中请求百度天气接口,得到该好友对应天气数据,解析处理数据,发送天气信息,完成该对象发送。
- 遍历结束,发送完毕
前期准备
- 在cmd命令中分别安装以下函数库
- 注意requests这个函数是在python安装路径的Scripts中安装,我们可以cmd命令进入这个路径去安装
完整代码实现
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177from wxpy import * import requests from datetime import datetime import time from apscheduler.schedulers.blocking import BlockingScheduler #定时框架 bot = Bot(cache_path=True) tuling = Tuling(api_key='4a0488cdce684468b95591a641f0971d') #机器人api location = '深圳' def send_weather(location): #准备url地址 path ='http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?' url = path % location response = requests.get(url) result = response.json() #如果城市错误就按照深圳发送天气 if result['error'] !=0: location ='深圳' url = path % location response = requests.get(url) result = response.json() str0 = (' 宝宝!这是明天的天气预报!n 来自您贴心的小棉袄 : 李老师n') results = result['results'] # 取出数据字典 data1 = results[0] # 取出城市 city = '深圳' str1 =' 你的城市: %sn' % city # 取出pm2.5值 pm25 = data1['pm25'] str2 =' Pm值 : %sn' % pm25 # 将字符串转换为整数 否则无法比较大小 pm25 = int(pm25) if pm25 =='': pm25 =0 # 通过pm2.5的值大小判断污染指数 if 0 <= pm25 <35: pollution ='优' elif 35 <= pm25 <75: pollution ='良' elif 75 <= pm25 <115: pollution ='轻度污染' elif 115 <= pm25 <150: pollution ='中度污染' elif 150 <= pm25 <250: pollution ='重度污染' elif pm25 >=250: pollution ='严重污染' str3 =' 污染指数: %sn' % pollution result1 = results[0] weather_data = result1['weather_data'] data = weather_data[1] datetime = data['date'] temperature = data['temperature'] str4 =' 明天温度: %s%sn' % (datetime,temperature) wind = data['wind'] str5 =' 风向 : %sn' % wind weather = data['weather'] str6 =' 天气 : %sn' % weather # str7 =' 温度 : %sn' % data['temperature'] message = data1['index'] str8 =' 穿衣 : %sn' % message[0]['des'] str9 =' 我很贴心: %sn' % message[2]['des'] str10 =' 运动 : %sn' % message[3]['des'] str11 =' 紫外线 : %sn' % message[4]['des'] str12="n爱你爱你爱你muamua~n" str = str0 + str1 + str2 + str3 + str4 + str5 + str6 + str8 + str9 + str10 + str11+ str12 return str #好友列表 #my_friends = [] #my_friends = bot.friends() #my_friends.pop(0) friend = bot.friends().search('ZzZero丶')[0]#好友的微信昵称,或者你存取的备注 print(friend) #发送函数 def send_message(): #给全体好友发送------------------ # for friend in my_friends: # friend.send(send_weather(friend.city)) #------------------------------------- #给单个好友发送消息----------------- location = '深圳' friend.send(send_weather(location)) #发送成功通知我 bot.file_helper.send(send_weather(location)) bot.file_helper.send('发送完毕') #定时器 print('start') #定时操作 #sched = BlockingScheduler() #sched.add_job(send_message,'cron',month='1-12',day='1-31',hour=6,minute =00)#设定发送的时间 #sched.start() send_message()#执行程序时直接发送
效果展示
最后
以上就是虚拟蜜蜂最近收集整理的关于Python 实现给微信好友定时发送天气预报的全部内容,更多相关Python内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复