python 实现微信发送消息
背景:利用Python来登入你个人的手机微信,之后向朋友发送消息,发送的消息可以来源于网页。下面的例子就是取得当前日元的汇率,之后发送自己的某一个朋友的手机上
环境:Python3,JetBrains PyCharm Community Edition 2018.2.4 x64,window7
实现的效果如下:

汇率的消息来源于网页

具体实现:
import json
from threading import Timer
from wxpy import *
import requests
import urllib.parse def get_news():
'''获取金山词霸每日一句'''
url = 'http://open.iciba.com/dsapi'
r = requests.get(url)
content = r.json()['content']
note = r.json()['note']
return content, note def send_news():
val = getRequest()
mssage = u'今天的日元汇率为:%s' % val
bot = Bot() # 连接微信,会出现一个登陆微信的二维码
try:
# my_friends =bot.friends().search(u'XXX')[0]#这里是你微信好友的昵称
friends = bot.friends()
for i in friends:
name = i
if "XXX" == name.name:
print(name)
else:
print(name)
break
# my_friend.send(contents[0])
# my_friend.send(contents[1])
name.send(mssage)
t = Timer(86400, send_news) # 这里是一天发送一次,86400s = 24h
t.start()
except:
my_friend = bot.friends().search('filehelper')[0]
my_friend.send(u'今天消息发送失败了') def getRequest():
_header = getHeader()
_data = urllib.parse.urlencode(getData()).encode('utf-8')
url = 'https://webapi.huilv.cc/api/trend/yaho'
response = requests.post(url, data=_data, headers=_header) print(response.encoding)
print(response.apparent_encoding)
r = response.text
result_text = json.loads(r, encoding='utf-8')
val = result_text["obj"][len(result_text["obj"]) -1 ]["huilv"]
return val def getHeader():
header = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Referer': 'https://www.huilv.cc/zoushitu?a=JPYCNY&time=d1',
'Sec-Fetch-Mode': 'cors',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'
}
return header def getData():
paydata = {
'pinzhong': 'JPYCNY',
'longs': 'd1'
}
return paydata if __name__ == '__main__':
get_news()
send_news()
python 实现微信发送消息的更多相关文章
- python实现通过企业微信发送消息
实现了通过企业微信发送消息,平时用于运维的告警还是不错的,相对于邮件来说,实时性更高,不过就是企业微信比较麻烦,此处不做过多解释. 企业微信api的详细请看:http://work.weixin.qq ...
- .netcore--Controller后台实现企业微信发送消息
一.获得企业微信管理端权限,登录企业企业微信管理端界面,并创建应用,如下图中的[网站消息推送] 二.参见企业微信API文献,根据corpid=ID&corpsecret=SECRET(其中企业 ...
- python实现微信发送服务器监控报警消息代码实现
这篇文章主要介绍了python3.8 微信发送服务器监控报警消息代码实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 ! python版本 > ...
- python实现定时发送消息
#!/usr/bin/env python#-*- coding:utf-8 -*- @Author : wujf @Time:2018/8/21 15:59# 自动发送消息from threadin ...
- python给邮箱发送消息
首先要用到两个模块 并且大同你的发送邮箱smtp 最开始测试没打通了好久 smtplib是提供邮箱smtp服务, email是提供你发送消息的格式之类服务 import smtplib from e ...
- python特定时间发送消息到微信公众号
#!/usr/bin/env python# -*- coding: utf-8 -*-# __author__ = 'James'# 导入模块from wxpy import *import tim ...
- python登录网页版微信发送消息
# coding=utf-8 import datetime import time from selenium import webdriver url = "https://wx2.qq ...
- python之微信自动发送消息
代码如下: from __future__ import unicode_literals from threading import Timer from wxpy import * import ...
- zabbix微信发送消息脚本
cat /usr/local/zabbix/share/zabbix/alertscripts/sed_messages_weixin.py python2.x #!/usr/bin/env pyth ...
随机推荐
- idea拉取最新代码弹窗(Ctrl + T)
在此设置
- 自然数幂和——第一类Stirling数和第二类Stirling数
第一类Stirling数 首先设 $$S_k(n)=\sum_{i=0}^ni^k$$ 根据第一类斯特林数的定义(P是排列数,C是组合数,s是Stirling) $$C_n^k={P_n^k\over ...
- 转成p进制算法C语言
今天打比赛的时候竟然下一没有想起来, 实际上是非常简单的. 举例说明: $64 = 2 \times 3^3 + 1 \times 3^2 + 3^0$ 根据秦九韶算法每次提出3,即 $3(2 \ti ...
- MySQL server has gone away && Lost connection to MySQL server during query
问题一.MySQL server has gone away ##### peewee from peewee import * from peewee import __exception_wrap ...
- LeetCode 449. Serialize and Deserialize BST
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...
- rhcsa备战笔记
笔记全部手打 转载请加原文链接 0)重置密码开机按e 找到linux16行 rd.break console=tty0 ctrl+xmount -o remount,rw /sysrootchroo ...
- bg/fg
将一个在后台暂停的命令,变成继续执行 (在后台执行). 一般ctrl+z就把前台命令调到了后台 将后台中的命令调至前台继续运行
- 搭建的一套vue打包方案,方便记录一下
package.json 配置如下: { "name": "rise-vue", "version": "1.0.0", ...
- BZOJ 1477: 青蛙的约会
二次联通门 : BZOJ 1477: 青蛙的约会 /* BZOJ 1477: 青蛙的约会 扩展欧几里得 列出方程, 判断一下 */ #include <iostream> #include ...
- 虚拟变量陷阱(Dummy Variable Trap)
虚拟变量陷阱(Dummy Variable Trap):指当原特征有m个类别时,如果将其转换成m个虚拟变量,就会导致变量间出现完全共线性的情况. 假设我们有一个特征“性别”,包含男性和女性两个类别,如 ...