多任务定时发送python3

 定时发送,天气,文本信息,发送给指定好友、群。

schedule定时
 1 import requests
 2 from requests import exceptions
 3 from urllib.request import urlopen
 4 from bs4 import BeautifulSoup
 5 import re
 6 from wxpy import *
 7 import  schedule
 8 import  time
 9  
10  
11 #bot=Bot(cache_path=True) #登陆网页微信,并保存登陆状态
12 bot = Bot(console_qr=2,cache_path="botoo.pkl")#Linux专用,像素二维码
13  
14 def sendblogmsg(content):
15     #搜索自己的好友,注意中文字符前需要+u
16     my_friend = bot.friends().search(u'王琳杰')[0]
17     my_friend.send(content)
18 
19     my1_friend = bot.friends().search(u'浮生若梦')[0]
20     my1_friend.send(content)
21 
22     my_group = bot.groups().search(u'聊天机器人测试')[0]
23     my_group.send(content) #发送天气预报
24 
25 
26 
27     my1_group = bot.groups().search(u'测试')[0]
28     my1_group.send(content) #发送天气预报
29  
30 def job():
31     resp=urlopen('http://www.weather.com.cn/weather/101010100.shtml')
32     soup=BeautifulSoup(resp,'html.parser')
33     tagToday=soup.find('p',class_="tem")  #第一个包含class="tem"的p标签即为存放今天天气数据的标签
34     try:
35         temperatureHigh=tagToday.span.string  #有时候这个最高温度是不显示的,此时利用第二天的最高温度代替。
36     except AttributeError as e:
37         temperatureHigh=tagToday.find_next('p',class_="tem").span.string  #获取第二天的最高温度代替
38  
39     temperatureLow=tagToday.i.string  #获取最低温度
40     weather=soup.find('p',class_="wea").string #获取天气
41     contents = '北京' + '\n' +  '最高温度:' + temperatureHigh + '\n' + '最低温度:' + temperatureLow + '\n' +  '天气:' + weather 
42        # result3 = '最低温度:' + temperatureLow
43     #print('最低温度:' + temperatureLow)
44     #print('最高温度:' + temperatureHigh)
45    # print('天气:' + weather)
46     sendblogmsg(contents)
47 
48 
49 def sendblogmsg_1(content):
50     #搜索自己的好友,注意中文字符前需要+u
51     my_friend = bot.friends().search(u'王琳杰')[0]
52     my_friend.send(content)
53 
54 def job_1():
55 
56     if bot == None:
57         login_wechat()
58 
59     contents = '早点休息,晚安😴' 
60        # result3 = '最低温度:' + temperatureLow
61     #print('最低温度:' + temperatureLow)
62     #print('最高温度:' + temperatureHigh)
63    # print('天气:' + weather)
64     sendblogmsg_1(contents)
65 
66 
67 
68 
69 #定时
70 schedule.every().day.at("22:50").do(job) #规定每天12:30执行job()函数
71 schedule.every().day.at("22:50").do(job_1) #规定每天12:30执行job()函数
72 while True:
73     schedule.run_pending()#确保schedule一直运行
74     time.sleep(1)
75 bot.join() #保证上述代码持续运行

 

posted @ 2018-07-12 20:57  王琳杰  阅读(492)  评论(0编辑  收藏  举报