侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

基于Python实现定时自动给微信好友发送天气预报

Python  /  管理员 发布于 7年前   143

效果图

from wxpyimport *import requestsfrom datetimeimport datetimeimport timefrom apscheduler.schedulers.blockingimport BlockingScheduler#定时框架bot = Bot(cache_path=True)tuling = Tuling(api_key=你的api')#机器人apidef send_weather(location):#准备url地址path ='http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'url = path % locationresponse = requests.get(url)result = response.json()#如果城市错误就按照濮阳发送天气if result['error'] !=0:location ='濮阳'url = path % locationresponse = requests.get(url)result = response.json()str0 = ('  早上好!这是今天的天气预报!……机器人:PyChatBot\n')results = result['results']# 取出数据字典data1 = results[0]# 取出城市city = data1['currentCity']str1 ='  你的城市: %s\n' % city# 取出pm2.5值pm25 = data1['pm25']str2 ='  Pm值  : %s\n' % pm25# 将字符串转换为整数 否则无法比较大小if pm25 =='':pm25 =0pm25 =int(pm25)# 通过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 ='  污染指数: %s\n' % pollutionresult1 = results[0]weather_data = result1['weather_data']data = weather_data[0]temperature_now = data['date']str4 ='  当前温度: %s\n' % temperature_nowwind = data['wind']str5 ='  风向  : %s\n' % windweather = data['weather']str6 ='  天气  : %s\n' % weatherstr7 ='  温度  : %s\n' % data['temperature']message = data1['index']str8 ='  穿衣  : %s\n' % message[0]['des']str9 ='  我很贴心: %s\n' % message[2]['des']str10 ='  运动  : %s\n' % message[3]['des']str11 ='  紫外线 : %s\n' % message[4]['des']str = str0 + str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8 + str9 + str10 + str11return str#好友列表my_friends = []my_friends = bot.friends()my_friends.pop(0)#发送函数def send_message():#给全体好友发送for friendin my_friends:friend.send(send_weather(friend.city))#发送成功通知我bot.file_helper.send(send_weather('濮阳'))bot.file_helper.send('发送完毕')#定时器print('star')sched = BlockingScheduler()sched.add_job(send_message,'cron',month='1-12',day='1-31',hour=5,minute =30)sched.start()

具体操作:

首先导入wxpy、图灵机器人和定时器Apscheduler,定时器用来定时群发。

具体pip操作建议百度。

使用百度的一个天气接口得到json数据。

主要思路:

1.从wxpy获取好友列表

2.创建定时器

3.定时器触发函数

4.函数执行,遍历好友列表

5.好友对象执行带参函数,参数为该好友城市

6.函数中请求百度天气接口,得到该好友对应天气数据,解析处理数据,发送天气信息,完成该对象发送。

7.遍历结束,发送完毕

缺陷:打包为exe文件之后启动失败,原因是定时器找不到trigger,要想解决需要查看Apscheduler相关资料。

解决方法:换一种定时器。

编译器上正常执行。

打包为exe之后,可以很方便发给别人使用。扫码登录后每天早上5:30会自动给所有好友发送效果图中的天气预报。

PS:下面看下Python实现微信定时发送天气预报

schedule实现定时

 import requestsfrom requests import exceptionsfrom urllib.request import urlopenfrom bs4 import BeautifulSoupimport refrom wxpy import *import scheduleimport timebot=Bot(cache_path=True) #登陆网页微信,并保存登陆状态def sendblogmsg(content):  #搜索自己的好友,注意中文字符前需要+u  my_friend = bot.friends().search(u'卿尘')[0]  my_friend.send(content)  #my_group = bot.groups().search(u'卿尘')[0]  #my_group.send(content) #发送天气预报def job():  resp=urlopen('http://www.weather.com.cn/weather/101010100.shtml')  soup=BeautifulSoup(resp,'html.parser')  tagToday=soup.find('p',class_="tem") #第一个包含class="tem"的p标签即为存放今天天气数据的标签  try:    temperatureHigh=tagToday.span.string #有时候这个最高温度是不显示的,此时利用第二天的最高温度代替。  except AttributeError as e:    temperatureHigh=tagToday.find_next('p',class_="tem").span.string #获取第二天的最高温度代替  temperatureLow=tagToday.i.string #获取最低温度  weather=soup.find('p',class_="wea").string #获取天气  contents = '北京' + '\n' + '最高温度:' + temperatureHigh + '\n' + '最低温度:' + temperatureLow + '\n' + '天气:' + weather     # result3 = '最低温度:' + temperatureLow  #print('最低温度:' + temperatureLow)  #print('最高温度:' + temperatureHigh)  # print('天气:' + weather)  sendblogmsg(contents)#定时schedule.every().day.at("19:20").do(job) #规定每天12:30执行job()函数while True:  schedule.run_pending()#确保schedule一直运行  time.sleep(1)bot.join() #保证上述代码持续运行

end

总结

以上所述是小编给大家介绍的基于Python实现定时自动给微信好友发送天气预报,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!


  • 上一条:
    python学习之hook钩子的原理和使用
    下一条:
    python批量修改图片后缀的方法(png到jpg)
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客