码迷,mamicode.com
首页 > 微信 > 详细

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

时间:2018-07-12 21:46:37      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:err   标签   urllib   ever   class   文字   contents   schedule   获取   

 

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() #保证上述代码持续运行

 

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

标签:err   标签   urllib   ever   class   文字   contents   schedule   获取   

原文地址:https://www.cnblogs.com/wanglinjie/p/9280992.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!