python打招呼函数_Python每天问候早安晚安
前言:
每天给你一句天气问候,晚上一句晚安。首要条件,你得有个女朋友。有天气预报,有鸡汤语句。这个例子可以学习的点是Python的请求接口和解析json数据requests,web的微信机器人itchat,定时任务schedule的运用的小例子,Python的语法缩进很严格,一半的错误来自缩进问题。
必要环境:
女朋友或者老婆
需要模块:
Python 2.7+、itchat、requests、schedule
教程:
安装itchatpip install itchat
安装requestspip install requests
安装schedulepip install schedule
代码:#coding:utf-8
import re
import time
import itchat
from itchat.content import *
import schedule
import requests
import sys
reload(sys)
sys.setdefaultencoding('utf-8') #由于我们返回的是中文,Unicode的编码问题,读取文件时使用的编码默认是ascii而不是utf8,所以这里我们要把默认编码设为utf8
# 天气的预报设置
def get_weather(url):
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36'
}
# 请求Weather API并拿到服务器返回的数据
rep = requests.get(url, headers = header)
rep.encoding = "utf-8"
result = ''
weather = rep.text
# 解析服务器返回的数据,具体可参考weather.json文件
index_cityInfo = weather.find("cityInfo")
index_cityId = weather.find("cityId")
index_shidu = weather.find("shidu")
index_pm25 = weather.find("pm25")
index_pm10 = weather.find("pm10")
index_quality = weather.find("quality")
index_wendu = weather.find("wendu")
index_ganmao = weather.find("ganmao")
index_forecast = weather.find("forecast")
index_ymd = weather.find("ymd", index_forecast)
index_week = weather.find("week", index_forecast)
index_sunset = weather.find("sunset", index_forecast)
index_high = weather.find("high", index_forecast)
index_low = weather.find("low", index_forecast)
index_fx = weather.find("fx", index_forecast)
index_fl = weather.find("fl", index_forecast)
index_aqi = weather.find("aqi", index_forecast)
index_type = weather.find("type", index_forecast)
index_notice = weather.find("notice", index_forecast)
# 将解析好的数据组装成想要的格式做为函数的返回值
'''
# 今日天气预报
# 年月日 + 星期 + 所在地城市
# 天气类型 + 风向 + 风力
# 温度范围(最低温度~最高温度)
# 污染指数:PM2.5/PM10/AQI
# 空气质量
# 当前温度 + 空气湿度
# Notice信息
'''
result = '今日天气预报' + '\n' \
+ weather[index_ymd + 6:index_week - 3] + " " \
+ weather[index_week + 7:index_fx - 3] + " " \
+ weather[index_cityInfo + 19:index_cityId - 3] + '\n' \
+ "天气: " + weather[index_type + 7:index_notice - 3] + " " \
+ weather[index_fx + 5:index_fl - 3] \
+ weather[index_fl + 5:index_type - 3] + '\n' \
+ "温度范围:" + weather[index_low + 9:index_sunset - 3] + " ~" \
+ weather[index_high + 10:index_low - 3] + '\n' \
+ "污染指数: PM2.5:" + weather[index_pm25 + 6:index_pm10 - 1] + " " \
+ "PM10:" + weather[index_pm10 + 6:index_quality - 1] + " " \
+ "AQI:" + weather[index_aqi + 5:index_ymd - 2] + '\n' \
+ "空气质量:" + weather[index_quality + 10:index_wendu - 3] + '\n' \
+ "当前温度:" + weather[index_wendu + 8:index_ganmao - 3] + " " \
+ "空气湿度:" + weather[index_shidu + 8:index_pm25 - 3] + '\n' \
+ weather[index_notice + 9:weather.find('}', index_notice) - 1]
return result
# 获取每天一句晚安话语
'''
#
#主要来源木芽一言
#https://xygeng.cn/170.html
#
'''
def get_news():
# 这里是把木芽每日一句中拿过来的信息发送给你朋友
url = "https://api.xygeng.cn/dailywd/api/"
r = requests.get(url)
content = r.json()['txt']
return content
# 早安的设置
def auto_send_morning():
# weather API的URL,此处的城市编号,参看_city.json
#101210101是城市代码
url = 'http://t.weather.sojson.com/api/weather/city/101210101'
# 调用get_weather函数
GW = get_weather(url)
# 发送微信消息
# 填入你朋友的微信备注
# 发送给备注早安晚安所有的用户
# 发送微信消息,中文随意修改
my_friends = itchat.search_friends('早安晚安')
for g in range(0,len(my_friends)):
itchat.send('早上好Y(^o^)Y,新的一天新气象ヽ(✿゚▽゚)ノ。',my_friends[g]['UserName'])
itchat.send(GW,my_friends[g]['UserName'])
# 每隔86400秒(1天),发送1次
# t = Timer(60, auto_send)
# t.start()
# 晚安的设置
def auto_send_evening():
# 发送微信消息
# 填入你朋友的微信备注
# 发送给备注早安晚安所有的用户
# 发送微信消息,中文随意修改
my_friends = itchat.search_friends('早安晚安')
for g in range(0,len(my_friends)):
itchat.send(get_news()+'。晚安咯,愿你好梦!(๑•̀ㅂ•́)و✧(๑•̀ㅂ•́)و✧',my_friends[g]['UserName'])
# 时间设置
def main():
# 早上8点
schedule.every().day.at("08:00").do(auto_send_morning)
# 晚上10点半
schedule.every().day.at("22:30").do(auto_send_evening)
while True:
schedule.run_pending()#确保schedule一直运行
time.sleep(1)
# 登录,仅支持能登录web版的微信号,新申请的账号一般不可以
if __name__ == '__main__':#启动微信自动登录,二维码登录
itchat.auto_login(hotReload=True, enableCmdQR=2)
main()
itchat.run()### unterminated keywords ###
使用:
1、将代码保存为zaoan.py
2、将文件放置目录下
3、配置好环境的服务器linux输入命令:screen -S wechatpython zaoan.py
4、扫码登录
5、直接后台按组合键Ctrl+a+d
6、退出,先找出你创建的screen,然后kill掉,第二行的41493需要自己修改screen -lsscreen -S 41493.wechat -X quit
python打招呼函数_Python每天问候早安晚安相关推荐
- python微信群定时发早安_Python每天定时发早安晚安语录
昨天在技术群里问大家七夕节礼物准备好了吗?大多数男程序员回复姿势都是这样的: 程序员有女朋友? new 一个就行. Python 只要内存够,想 new 多少个对象都不是问题. 由于行业环境的原因,程 ...
- Python每天定时发早安晚安语录
此文首发于公众号「Python知识圈」,欢迎直接去公众号看. " 阅读文本大概需要 3.7 分钟 昨天在技术群里问大家七夕节礼物准备好了吗?大多数男程序员回复姿势都是这样的: 程序员有女朋友 ...
- python not函数_python 函数
1 为什么使用函数 在没有接触函数时,有时候需要将一个功能多次写,如果需要修改其中一个变量,则需要把所有实现该功能的代码一处一处改.不利于代码维护,代码量大了,组织结构也会很不清晰. 所以总结不使用函 ...
- python include函数_python 库函数
python的内建函数和库函数的区别是什么? [区别]:标准库函数都需要import xxx才能取得.内建函数都在__builtins__里面,在global里直接就能用. [补充]:1.python ...
- python islower函数_python字符串是否是小写-python 字符串小写-python islower函数-python islower函数未定义-嗨客网...
Python字符串是否是小写教程 在开发过程中,有时候我们需要判断一个 Python islower()函数详解 语法 str.islower() -> bool 参数 参数 描述 str 表示 ...
- python agg函数_Python Pandas Series.agg()用法及代码示例
Python是进行数据分析的一种出色语言,主要是因为以数据为中心的python软件包具有奇妙的生态系统. Pandas是其中的一种,使导入和分析数据更加容易. Pandas Series.agg()用 ...
- python findall函数_python正则表达式之中的findall函数是什么?
在这篇文章之中我们来了解一下关于python正则表达式的相关知识,有些朋友可能是刚刚接触到python这一编程语言,对这一方面不是特别的了解,在接下来这篇文章将会来带大家来了解关于正则表达式中的pyt ...
- python log函数_Python log10() 函数
Python log10() 函数 描述 log10() 方法返回以10为基数的x对数. 语法 以下是 log10() 方法的语法:import math math.log10( x ) 注意:log ...
- python value函数_python 函数基础
什么是函数? 函数在Python中是最基本的程序结构,用来最大化地让我们的代码进行复用.简单地说,一个函数就是一组Python语句的组合,它们可以在程序中运行一次或多次运行.Python中的函数在其他 ...
最新文章
- nginx无法启动异常
- ACM模板--邻接矩阵 有向图
- 13SpringMvc_限定某个业务控制方法,只允许GET或POST请求方式访问
- Apache配置虚拟主机三大问题--自己的相关坑
- c语言灯塔案例求塔低数,C++:有一个8层灯塔,每层所点灯数都等于该层上一层的两倍,一共有765盏灯,求塔底的灯数...
- java四神兽_SpringCloud五大神兽之Eureka
- HEVC码流简单分析
- 【转】java获取当前路径的几种方法
- 数据挖掘 姓名用字特点 目录 1. 姓名用字特点	1 2. 男性姓名专用字210个(三字词,双字词都适用)	1 2.1. 男性姓名专用字 双字词适用317个	1 3. 女人姓名用字	2 3.1.
- scanf()接受不同类型的参数的一个例子
- 牛客网剑指offer
- 笔记本设置wifi热点并抓包
- web自动化:web控件交互操作/多窗口处理/网页frame
- WebSocket协议数据格式解析
- 计算机面试专业英语词汇,英语面试中常用高频词汇
- 抖音网红追女生小程序代码
- 在Excel中如何制作K线
- java 保存在_Java存储到什么地方
- 如何修改python中字典的键和值
- html5 圆圈扩散,CSS3地图动态实例代码(圆圈向外扩散)
热门文章
- 2019年青少年编程c语言考试,全国青少年软件编程(C语言)等级考试试题 2019年9月(一级含答案)...
- 社交IM系统三大好处——社交IM系统搭建要具备哪些“特有”功能?
- HTML5+JavaScript动画基础 完整版 中文pdf扫描版
- 会长得了绝症(转自NGA)
- 记录-mysql操作-crontab操作
- 网络恐怖袭击真实存在吗
- 学校的计算机考试是几级a,职称计算机考试A、B、C、D各级别是怎么划分的?
- LeetCode11.盛最多水的容器
- Google开源项目WebRTC下载及编译
- Redis高可用(四)- 哨兵