超简单!只需简单几步即可为TA定制天气小助理!!
前提虽然我们每天都会查看天气,但是如果你能够用限制微信每天早上七点定时为TA推送天气预报,生活指数。直接为TA提供过滤好的天气信息,让TA一目了然。而且操作简单上手,任何人都可以零基础搭建,为你的TA带来一丝小惊喜。 效果如图准备
主要代码1. 安装 python 3.7 版本小伙伴如果是 window 电脑可以直接在 Python官网 选择 download 直接下载 直接点击下载好的exe文件进行安装,记得将环境变量配置选项勾选上。如下图: 安装完成之后,我们可以新建文件夹,从git 上将代码clone 下来。我们直接在文件夹中摁住 shift + 鼠标右键 唤出 powerShell 查看 Python 版本。 如果大家还有不明白的可以参考 Python安装教程 2. 为项目安装相关模块
#coding=utf8 import requests from urllib.request import urlopen from bs4 import BeautifulSoup from urllib.parse import urlencode from threading import Timer import re from wxpy import * import schedule import time import http import json import datetime import random 重要: 其中 wxpy 模块是本项目以及后续项目中很重要的模块,就是因为这个开源项目,我们才能使其与微信产生交互。 wxpy控制路由器、智能家居等具有开放接口的玩意儿 运行脚本时自动把日志发送到你的微信 加群主为好友,自动拉进群中 跨号或跨群转发消息 自动陪人聊天 逗人玩 ... 项目介绍以及安装方法,大家可以去主页想看wxpy 3. 初始化机器人参考 登录文档 bot = Bot(cache_path=True,console_qr = 1) // 初始化机器人,扫码登录 console_qr 是用于登录的二维码展示默认为1 bot.enable_puid('wxpy_puid.pkl') // 机器人启用 puid 属性,并指定 puid 所需的映射数据保存/载入路径 4. 设置模拟请求接口相关def api(url): header = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8','Accept-Encoding': 'gzip,deflate','Accept-Language': 'zh-CN,zh;q=0.8','Connection': 'keep-alive','User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/43.0.235' } timeout = random.choice(range(80,180)) data = requests.get(url,headers=header,timeout=timeout) return data.json() header 中用来设置模拟请求接口的浏览器相关参数防止api 以为我们是爬虫抓取。并设定随机请求时间。 5. 设置相关请求,以及需要的信息字段拼接def sendweather(city,xx): url = 'https://free-api.heweather.com/s6/weather/forecast?location='+city+'&key=和风key' PMurl = 'https://free-api.heweather.com/s6/air/now?parameters&location='+city+'&key=和风key' lifeurl = 'https://free-api.heweather.com/s6/weather/lifestyle?location='+city+'&key=和风key' temp = api(url) temp = temp['HeWeather6'][0] update = temp['update'] now = temp['daily_forecast'][0] nowTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') pm = api(PMurl) pm = pm['HeWeather6'][0] airnow = pm['air_now_city'] life = api(lifeurl) life = life['HeWeather6'][0] life = life['lifestyle'] result = xx + city +' ---' + 'n'+ 'n' + ' 今天天气:'+ now['cond_txt_d'] + ' 转 ' + now['cond_txt_n'] + 'n' + ' 今天温度:'+ now['tmp_min'] + '°C ~ ' + now['tmp_max'] + '°C' + 'n' + ' 风向:'+ now['wind_dir'] + ' ' + now['wind_sc'] + '级 '+ now['wind_spd'] + '公里/小时'+ 'n' + ' 相对湿度:'+ now['hum'] + '%' + 'n' + ' 降水量:'+ now['pcpn'] + 'ml' + ',降水概率:'+ now['pop'] + '%' + 'n' + ' 能见度:'+ now['vis'] + '公里' + 'n' + '------------------------------------------' + 'n' + '今天空气质量:'+'n' + ' 空气质量指数:'+ airnow['aqi']+'n' + ' 主要污染物:'+ airnow['main']+'n' + ' 空气质量:'+ airnow['qlty']+'n' + ' 二氧化氮指数:'+ airnow['no2']+'n' + ' 二氧化硫指数:'+ airnow['so2']+'n' + ' 一氧化碳指数:'+ airnow['co']+'n' + ' pm10指数:'+ airnow['pm10']+'n' + ' pm25指数:'+ airnow['pm25']+'n' + ' 臭氧指数:'+ airnow['o3'] +'n' + '------------------------------------------' + 'n' + '1、'+ life[0]['txt']+'nn' + '2、'+ life[1]['txt']+'nn' + '3、'+ life[2]['txt']+'nn' + '????????????????????'+'nn' result = result + '发送时间:' + nowTime + 'n' return result 小伙伴可以在和风官网注册账户,其中有免费的天气、生活指数api可以使用。免费的已经可以满足我们的需求。 6. 获取发送对象def auto_send(msg): weather = sendweather('苏州',msg) // 用来请求刚刚封装的请求天气 Lie = bot.friends().search(u'Lie')[0] // 查找你要发送的对象,必须在你的好友列表里也可以为群 后面是好友的昵称,记得不是备注名哦! WxpyChat = bot.groups().search('?? (?? . ??) ') // 获取要发送的群 Lie.send(weather) // 发送 WxpyChat.send(weather) // 可以设置多个发送对象 7. 设置定时发送事件schedule.every().day.at("13:56").do(auto_send,'早上好,') schedule.every().day.at("13:57").do(auto_send,'晚上好,') 8. 为程序设置持续运行保证项目一直在线while True: schedule.run_pending() time.sleep(1) 9. 至此,一个简单的天气小助理就完成了,可以按照以上步骤来实验一下。10. 扩展
Lie = bot.friends().search(u'Lie') @bot.register(Lie) // 注册对哪个好友的回复生效 def auto_reply_all(msg): if '苏州' in msg.text: nowWeather = sendweather(msg.text,msg) msg.sender.send(nowWeather) git
wepython 注意事项
下期预告用python + wxpy 为TA定制贴心喝水小助理。 关于我小夭同学,页面切图仔。 想体验机器人的小伙伴可以加下面二维码,验证信息 【py】 自动通过拉你入群体验机器人其他功能。 欢迎小伙伴关注我的微信 【小夭同学】 一起学习前端相关知识。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |