python发微信提醒天气_基于Python实现定时自动给微信好友发送天气预报
效果图
from wxpyimport *
import requests
from datetimeimport datetime
import time
from apscheduler.schedulers.blockingimport BlockingScheduler#定时框架
bot = Bot(cache_path=True)
tuling = Tuling(api_key=你的api')#机器人api
def send_weather(location):
#准备url地址
path ='http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'
url = path % location
response = requests.get(url)
result = response.json()
#如果城市错误就按照濮阳发送天气
if result['error'] !=0:
location ='濮阳'
url = path % location
response = 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 =0
pm25 =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' % pollution
result1 = results[0]
weather_data = result1['weather_data']
data = weather_data[0]
temperature_now = data['date']
str4 =' 当前温度: %s\n' % temperature_now
wind = data['wind']
str5 =' 风向 : %s\n' % wind
weather = data['weather']
str6 =' 天气 : %s\n' % weather
str7 =' 温度 : %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 + str11
return 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 requests
from requests import exceptions
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
from wxpy import *
import schedule
import time
bot=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实现定时自动给微信好友发送天气预报,希望对各位网友能有帮助,有任何疑问或建议请关注我们公众号(shtmlnet)给我们留言,。。在这十分感谢大家对学猫在线(shtml.net)网站的支持!
本文来源:https://www.jianshu.com/p/2c62e684941f
python发微信提醒天气_基于Python实现定时自动给微信好友发送天气预报相关推荐
- python发微信提醒天气_通过Python发送天气信息给企业微信机器人
一.添加机器人 原文链接:https://www.dqzboy.com 二.创文章来源(Source):浅时光博客建py脚本 #!/usr/bin/python3 # -*- coding: utf- ...
- 基于python爬虫技术的应用_基于Python爬虫技术的应用
办公自动化杂志 一.引言 本文主要是对 Python 爬虫技术进行阐述,基于 python 的爬虫与其他语言相比的有很多优势.通过爬去某个网站的所有新闻这个案例,来进一步阐释 Python 爬虫技术的 ...
- python实现空气质量提醒程序_基于Python实现空气质量指数可视化
前面我们已经爬取了全国城市空气质量数据( 基于Python实现城市空气质量爬取 ),基于之前我们爬取的数据,本文将使用Python将空气质量最好的前20个城市以柱状图的形式展示出来,点击对应的柱状图能 ...
- python开发酷q插件_基于python和酷Q的QQ机器人开发实践(1)
基本框架 (1) 酷Q:目前最常用的QQ机器人软件(尤其是在smartQQ失效以后). (2) 酷Q HTTP插件:通过HTTP对酷Q的事件进行上报以及接收HTTP请求来调用酷Q的DLL接口,从而可以 ...
- python网络爬虫的流程图_基于Python的网络爬虫的设计与实现
龙源期刊网 http://www.qikan.com.cn 基于 Python 的网络爬虫的设计与实现 作者:高祖彦 来源:<商情> 2020 年第 33 期 [摘要]一个爬虫从网上爬取数 ...
- python数字识别kaggle论文_基于Python语言Kaggle的数据集分析
界 基于 Python 语言 Kaggle 的数 据集 分析 段 聪 聪 柴 世 一 * 渊 河 南 大 学 计 算 机 与 信 息 工 程 学 院 袁 河 南 开 封 475004 冤 揖 摘 要 ...
- python是商业组织吗_基于Python的电子商务系统的弹性架构与思考
DOI:10. 19392 / j. cnki. 1671-7341. 201815075 基于 Python 的电子商务系统的弹性架构与思考 谢钟扬 湖南软件职业学院 湖南湘潭 411100 摘 要 ...
- python制作物联网控制软件下载_基于Python和Django框架的物联网智能设备管理系统的设计与实现...
论文写作指导:请加QQ229366758 基于Python和Django框架的物联网智能设备管理系统的设计与实现 作者:未知 摘 要:针对目前日益增多的智能设备提出了兼容性好,稳定性高,易于管理的管理 ...
- python卡方检验筛选特征原理_基于Python的遥感特征筛选—递归特征消除(RFE)与极限树(Extra-Trees)...
引言 基于前几篇文章关于筛选方法的介绍,本篇同样给大家介绍两种python封装的经典特征降维方法,递归特征消除(RFE)与极限树(Extra-Trees, ET).其中,RFE整合了两种不同的超参数, ...
最新文章
- 郑州尚学堂:JAVA常用4种排序方法
- [Swift]LeetCode732. 我的日程安排表 III | My Calendar III
- c语言计算文件摘要值,c语言文件操作摘要.ppt
- 交换机和路由器的区别是什么?
- range函数python2和3区别_【后端开发】range函数python2和3区别
- sqlserver两个日期之间的年数_SQL语句计算两个日期之间有多少个工作日的方法
- 用十万级数据进行讲解MySQL索引基础
- 汇编语言二进制转十进制_汇编语言笔记(一)——基础知识
- 人事管理系统(毕业设计)
- pjsip 录音生成wav文件声音一直很小
- .ftl文件简介及语法
- activex chrome插件_10个超实用的设计师专属Chrome小插件
- crucible VS gerrit
- stm32增量式编码器使用流程
- windows 离线安装nessus
- flask keras 多线程环境下加载模型
- 老贾笔记--轻轻松松oracle之logmnr篇[吃顿饭的功夫学恢复误删除(delete)的数据]
- python基础编程
- Oracle数据库中分区表的操作方法
- 微信小程序 python 自动化测试_微信小程序的自动化测试框架