Python实现微信定时发送天气预报

 

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  
13 def sendblogmsg(content):
14     #搜索自己的好友,注意中文字符前需要+u
15     my_friend = bot.friends().search(u'卿尘')[0]
16     my_friend.send(content)
17     #my_group = bot.groups().search(u'卿尘')[0]
18     #my_group.send(content) #发送天气预报
19  
20 def job():
21     resp=urlopen('http://www.weather.com.cn/weather/101010100.shtml')
22     soup=BeautifulSoup(resp,'html.parser')
23     tagToday=soup.find('p',class_="tem")  #第一个包含class="tem"的p标签即为存放今天天气数据的标签
24     try:
25         temperatureHigh=tagToday.span.string  #有时候这个最高温度是不显示的,此时利用第二天的最高温度代替。
26     except AttributeError as e:
27         temperatureHigh=tagToday.find_next('p',class_="tem").span.string  #获取第二天的最高温度代替
28  
29     temperatureLow=tagToday.i.string  #获取最低温度
30     weather=soup.find('p',class_="wea").string #获取天气
31     contents = '北京' + '\n' +  '最高温度:' + temperatureHigh + '\n' + '最低温度:' + temperatureLow + '\n' +  '天气:' + weather 
32        # result3 = '最低温度:' + temperatureLow
33     #print('最低温度:' + temperatureLow)
34     #print('最高温度:' + temperatureHigh)
35    # print('天气:' + weather)
36     sendblogmsg(contents)
37 #定时
38 schedule.every().day.at("19:20").do(job) #规定每天12:30执行job()函数
39 while True:
40     schedule.run_pending()#确保schedule一直运行
41     time.sleep(1)
42 bot.join() #保证上述代码持续运行

 

转载于:https://www.cnblogs.com/wanglinjie/p/9280992.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值