因为之前想过 如果每天早上微信能够发送天气预报给我,给我老婆多好,然后就动手看网上的教程做了一个可以定时发送天气预报的程序,

最近又想到折腾,做了一个更加详细的版本。但是需要主动操作

具体操作看图。

4bf724fa5a27670491cee4fd3b7977c.png

d3d3306aa96844c06a4dd991a398e2b.png

b0d0d26535ef1647a74fb0ec5853b5b.png

部分代码:

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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#coding=utf8
import requests
from requests import exceptions
from urllib.request import urlopen
from bs4 import BeautifulSoup
from urllib.parse import urlencode
from threading import Timer
import re
from wxpy import *
import schedule
import time
import http
import json
import datetime
import random

bot = Bot(cache_path=True,console_qr = 1)
myself = bot.self
bot.enable_puid('wxpy_puid.pkl')
tuling = Tuling(api_key='换成自己的图片key')
group = bot.groups().search(u'Test')
shgroup = bot.groups().search('伐木累🌱')
friends = bot.friends().search(u'Lie')
msgText = "Helo! 回复'功能'获取对应功能\n1.天气(例:苏州天气)\n2.今日nba(注:今日所有比赛结果)\n3.今日黄历\n4.每日一句\n5.开启机器人(关闭机器人)\n6.今日古诗词\n7.每日阅读\n8.历史上的今天\n9.nba排名(注:当日东西部排名)\n10.新闻\n 1.头条新闻\n 2.社会新闻\n 3.娱乐新闻\n 4.体育新闻\n11.星座运势(例如:天秤座)" #任意回复获取的菜单
newText = "你可以这样回复: \n1.头条新闻\n2.社会新闻\n3.娱乐新闻\n4.体育新闻"

def get_now_weather(city):
header = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.235'
}
url = 'https://free-api.heweather.com/s6/weather/now?location='+city+'&key=换成自己的聚合数据key'
PMurl = 'https://free-api.heweather.com/s6/air/now?parameters&location='+city+'&key=换成自己的和风key'
# 设定超时时间,防止被网站认为是爬虫
timeout = random.choice(range(80, 180))
rep = requests.get(url, headers=header, timeout=timeout)
pm = requests.get(PMurl, headers=header, timeout=timeout)
result = ''
temp = rep.json()
temp = temp['HeWeather6'][0]
update = temp['update']
now = temp['now']
nowTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
pm = pm.json()
pm = pm['HeWeather6'][0]
print(now)
airnow = pm['air_now_city']
result = city + '实时天气预报-' + '\n'\
+ '更新时间:'+ update['loc'] + '\n'\
+ ' 当前天气:'+ now['cond_txt'] + '\n'\
+ ' 当前温度:'+ now['tmp'] + '°C' + '\n'\
+ ' 体感温度:'+ now['fl'] + '°C' + '\n'\
+ ' 风向:'+ now['wind_dir'] + ' ' + now['wind_sc'] + '级 '+ now['wind_spd'] + '公里/小时'+ '\n'\
+ ' 相对湿度:'+ now['hum'] + '%' + '\n'\
+ ' 降水量:'+ now['pcpn'] + 'ml' + '\n'\
+ ' 能见度:'+ now['vis'] + '公里' + '\n'\
+ ' 云量:'+ now['cloud'] + '\n'\
+ '-----------------------------------' + '\n'\
+ '当前空气质量:'+'\n'\
+ ' 空气质量指数:'+ airnow['aqi']+'\n'\
+ ' 主要污染物:'+ airnow['main']+'\n'\
+ ' 空气质量:'+ airnow['qlty']+'\n'\
+ ' 二氧化氮指数:'+ airnow['no2']+'\n'\
+ ' 二氧化硫指数:'+ airnow['so2']+'\n'\
+ ' 一氧化碳指数:'+ airnow['co']+'\n'\
+ ' pm10指数:'+ airnow['pm10']+'\n'\
+ ' pm25指数:'+ airnow['pm25']+'\n'\
+ ' 臭氧指数:'+ airnow['o3']+'\n'

result = result + '发送时间:' + nowTime + '\n'

return result

def get_news(type):
header = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.235'
}
url = 'http://v.juhe.cn/toutiao/index?type='+str(type)+'&key=换成自己的聚合数据key'
timeout = random.choice(range(80, 180))
rep = requests.get(url, headers=header, timeout=timeout)
data = json.loads(rep.text)
data = data['result']
data = data['data']
item = []
obj = {}
html = '今日'+str(type)+'新闻:'+ '\n'
for i in data:
html = html + '标题:' + i['title'] + '\n'\
+ '链接:' + i['url'] + '\n'\
+ '分类:' + i['category'] + '\n'\
+ '来自:' + i['author_name'] + '\n'\
+ '时间:' + i['date'] + '\n'\
+ '-----------------------------------------------' + '\n' +'\n' \

return html

def get_star(name):
header = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.235'
}
url = 'http://web.juhe.cn:8080/constellation/getAll?consName='+str(name)+'&type=today&key=换成自己的聚合数据key'
timeout = random.choice(range(80, 180))
rep = requests.get(url, headers=header, timeout=timeout)
data = json.loads(rep.text)
starhtml = '今日'+str(name)+'运势:'+ '\n'\
+ ' 综合指数:' + data['all'] + '\n'\
+ ' 幸运色:' + data['color'] + '\n'\
+ ' 健康指数:' + data['health'] + '\n'\
+ ' 爱情指数:' + data['love'] + '\n'\
+ ' 财运指数:' + data['money'] + '\n'\
+ ' 速配星座:' + data['QFriend'] + '\n'\
+ ' 工作指数:' + data['work'] + '\n'\
+ ' 今日概述:' + data['summary'] + '\n'\

return starhtml

def get_nba():
resp = urlopen('https://m.hupu.com/nba/game')
soup = BeautifulSoup(resp,'html.parser')
tagToday = soup.find('section',class_="match-today")
nbaHtml = '今日NBA比赛结果:' + '\n' + '\n'
for tag in tagToday.find_all('a', class_='match-wrap'):
nbaHtml = nbaHtml + tag.find('div', class_='away-team').span.get_text() + ' ' + tag.find('strong', class_='').span.get_text() + ' ' + tag.find('div', class_='home-team').span.get_text() + ' (' + tag.find('div', class_='match-status-txt').get_text() +')' + '\n'

return nbaHtml

def get_rank():
resp = urlopen('https://m.hupu.com/nba/stats')
soup = BeautifulSoup(resp,'html.parser')
east = soup.find_all('li',class_= "weast")[0]
west = soup.find_all('li',class_= "weast")[1]
rankHtml = '今日NBA东部排名:(1.排名 2.球队 3.胜负 4.胜负差 5.最近情况)' + '\n' + '\n'
for tag in east.find_all('li', class_=''):
list = tag.find('p', class_='right-data')
rankHtml = rankHtml + tag.find('span', class_='rank').get_text() + '. ' + tag.find('div', class_='').h1.get_text() + ' ' + list.find_all('span')[0].get_text() + ' ' + list.find_all('span')[1].get_text() +' '+ list.find_all('span')[2].get_text() +'\n'

rankHtml = rankHtml + '\n' + '\n' + '---------------------------------------------' + '\n' + '\n'
rankHtml = rankHtml + '今日NBA西部排名:(1.排名 2.球队 3.胜负 4.胜负差 5.最近情况)' + '\n' + '\n'
for tag in west.find_all('li', class_=''):
list = tag.find('p', class_='right-data')
rankHtml = rankHtml + tag.find('span', class_='rank').get_text() + '. ' + tag.find('div', class_='').h1.get_text() + ' ' + list.find_all('span')[0].get_text() + ' ' + list.find_all('span')[1].get_text() +' '+ list.find_all('span')[2].get_text() +'\n'

return rankHtml

def invite(user):
print('4')
group = bot.groups().search('cc')
group[0].add_members(user, use_invitation=True)


@bot.register(msg_types=FRIENDS)
@bot.register(group)
@bot.register(shgroup,TEXT)
@bot.register(friends)


def auto_reply_all(msg):
if '苏州天气' in msg.text:
nowWeather = get_now_weather('苏州')
msg.sender.send(nowWeather)
elif 'py' in msg.text.lower():
# 接受好友 (msg.card 为该请求的用户对象)
new_friend = bot.accept_friend(msg.card)
# 或 new_friend = msg.card.accept()
# 向新的好友发送消息
new_friend.send('哈哈,我自动接受了你的好友请求,发送【帮助】获取帮助!!')
elif '上海天气' in msg.text:
nowWeather = get_now_weather('上海')
msg.sender.send(nowWeather)
elif '帮助' in msg.text:
msg.sender.send(msgText)
elif '每日一句' in msg.text:
url = 'http://open.iciba.com/dsapi/'
r = requests.get(url)
content = json.loads(r.text)
msg.sender.send('每日一句:\n'+content['content'] +'\n'+content['note']+"\n")
elif '今日黄历' in msg.text:
header = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.235'
}
timeout = random.choice(range(80, 180))
wk = {"Sunday":"星期日","Monday":"星期一","Tuesday":"星期二","Wednesday":"星期三","Thursday":"星期四","Friday":"星期五","Saturday":"星期六"}
yy = {"False":"不是","True":"是"}
url = "https://www.sojson.com/open/api/lunar/json.shtml"
r = requests.get(url, headers= header, timeout=timeout)
jsonarr = json.loads(r.text)
result = jsonarr["data"]
xingqi = str(wk[result['week']])
yangli = '阳历日期:' + str(result['year']) +'-'+ str(result['month']) +'-'+ str(result['day'])
nongli = '农历日期:' + str(result['cnyear']) + '-' + str(result['cnmonth']) + '月-' + str(result['cnday']) +'日'
jieqibody = result["jieqi"]
jieqi ='本月节气:'
for i in jieqibody:
jieqi += i + '日' + jieqibody[i] + ','
leap = '是否是闰月:' + str(result['leap'])
maxDayInMonth = '农历当月天数:' + str(result['maxDayInMonth'])
festivalList = '当天节日:' + str(result['festivalList'])
jiazi = '甲子:' + str(result['cyclicalYear']) + '-' + str(result['cyclicalMonth']) + '-' + str(result['cyclicalDay'])
shengxiao = '今年生肖:' + str(result['animal'])
yi = '宜:' + str(result['suit'])
ji = '忌:' + str(result['taboo'])
msg.sender.send('今天是:'+ xingqi + '\n' + '本日黄历:' + '\n' + yangli + '\n' + nongli + "\n" + jiazi + "\n" + shengxiao + "\n" + yi + "\n" + ji + "\n" + leap + "\n" + maxDayInMonth + "\n" + festivalList + "\n" + jieqi + "\n")
elif '今日古诗词' in msg.text:
header = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.235'
}
timeout = random.choice(range(80, 180))
url = "https://v2.jinrishici.com/info"
urlA = "https://v2.jinrishici.com/one.json"
r = requests.get(url, headers= header, timeout=timeout)
rA = requests.get(urlA, headers= header, timeout=timeout)
jsonarr = json.loads(r.text)
jsonA = json.loads(rA.text)
result = jsonarr["data"]
resultA = jsonA["data"]
content = str(resultA['content'])
origin = resultA['origin']
title = str(origin['title'])
dynasty = str(origin['dynasty'])
author = str(origin['author'])
detail = '《' + ',\n'.join(origin['content']) + '》'
matchTags = '推荐关键词:' + ','.join(resultA['matchTags'])
sendTime = '发送时间:' + str(resultA['cacheAt'])
ipAddress = '查询人IP:' + str(result['ip'])
address = '查询人地址:' + str(result['region'])
weatherData = result['weatherData']
wd = ' 实时温度:' + str(weatherData['temperature']) + '°C'
fx = str(weatherData['windDirection'])
fl = str(weatherData['windPower'])
sd = ' 湿度:' + str(weatherData['humidity'])
uptime = ' 更新时间:' + str(weatherData['updateTime'])
we = ' 天气:' + str(weatherData['weather'])
njd = ' 能见度:' + str(weatherData['visibility'])
yu = ' 雨量:' + str(weatherData['rainfall'])
pm25 = ' pm2.5:' + str(weatherData['pm25'])
tags = ' ' + ','.join(result['tags'])
scMsg = content + '\n' + ' ---- 这是来自' + dynasty + '诗人' + author + '的' + '《'+ title +'》' + '\n' + matchTags + '\n' +'诗句详情:' + '\n' + detail + '\n' + '\n' + sendTime + '\n' + ipAddress + '\n' + address + '\n' + '天气(实时):' + '\n' + we + '\n' + wd + '\n' + ' 风向:' + fx + fl + '\n' + sd +'\n' + njd + '\n' + yu + '\n' + pm25 + '\n' + '标签:' + '\n' + tags
msg.sender.send(scMsg)
elif '帮助' in msg.text:
msg.sender.send(msgText)
elif '今日nba' in msg.text:
nba = get_nba()
msg.sender.send(nba)
elif 'nba排名' in msg.text:
nbarank = get_rank()
msg.sender.send(nbarank)
elif '头条新闻' in msg.text:
nlist = get_news('头条')
msg.sender.send(nlist)
elif '娱乐新闻' in msg.text:
nlist = get_news('娱乐')
msg.sender.send(nlist)
elif '体育新闻' in msg.text:
nlist = get_news('体育')
msg.sender.send(nlist)
elif '社会新闻' in msg.text:
nlist = get_news('社会')
msg.sender.send(nlist)
elif '每日阅读' in msg.text:
header = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) A<Appl></Appl>WebKit/537.36 (KHTML, like Gecko) Chrome/43.0.235'
}
timeout = random.choice(range(80, 180))
url = "https://interface.meiriyiwen.com/article/random?dev=1"
r = requests.get(url, headers= header, timeout=timeout)
jsonarr = json.loads(r.text)
result = jsonarr["data"]
form = '来自:' + str(result['author']) + '的《' + str(result['title']) + '》\n'
content = str(result['content'])
msg.sender.send(form + content)
elif '历史上的今天' in msg.text:
header = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.235'
}
timeout = random.choice(range(80, 180))
m = datetime.datetime.now().month
d = datetime.datetime.now().day
url = "http://api.juheapi.com/japi/toh?v=1.0&month="+str(m)+"&day="+str(d)+"&key=换成自己的key"
r = requests.get(url, headers= header, timeout=timeout)
jsonarr = json.loads(r.text)
result = jsonarr["result"]
form = '历史上的【' + str(m) +'-'+ str(d)+ '】发生了以下事件:' + '\n' + '\n'
item = []
for i in result:
item.append(i['des'])
item = '\n'+ '\n'.join(item)

msg.sender.send(form + item)
elif '白羊座' in msg.text:
star = get_star('白羊座')
msg.sender.send(star)
elif '金牛座' in msg.text:
star = get_star('金牛座')
msg.sender.send(star)
elif '双子座' in msg.text:
star = get_star('双子座')
msg.sender.send(star)
elif '巨蟹座' in msg.text:
star = get_star('巨蟹座')
msg.sender.send(star)
elif '狮子座' in msg.text:
star = get_star('狮子座')
msg.sender.send(star)
elif '处女座' in msg.text:
star = get_star('处女座')
msg.sender.send(star)
elif '天秤座' in msg.text:
star = get_star('天秤座')
msg.sender.send(star)
elif '天蝎座' in msg.text:
star = get_star('天蝎座')
msg.sender.send(star)
elif '射手座' in msg.text:
star = get_star('射手座')
msg.sender.send(star)
elif '摩羯座' in msg.text:
star = get_star('摩羯座')
msg.sender.send(star)
elif '水瓶座' in msg.text:
star = get_star('水瓶座')
msg.sender.send(star)
elif '双鱼座' in msg.text:
star = get_star('双鱼座')
msg.sender.send(star)
elif '开启机器人' in msg.text:
msg.sender.send('哎呀,人家还不能这样呢')
elif '关闭机器人' in msg.text:
msg.sender.send('哎呀,人家还不能这样呢')
elif '加群' in msg.text.lower():
print('1')
group = bot.groups().search('cc')
group[0].add_members(msg.sender, use_invitation=True)
else:
tuling.do_reply(msg)
msg.sender.send(msg.location)

while True:
schedule.run_pending()
time.sleep(1)

喜欢的可以加微信【wex_5201314】验证信息【py】拉你进群体验

或者关注公众号 【故事胶片】 发送 【wxpy】获取源码

或者直接扫描二维码

WeChat Image_20181221112044.jpg