* 给公众号发送特定文字
* 普通的网页点击
* 授权的网页点击
我这里给出前两种的解决方案。
普通的网页点击:
1. 建议通过浏览器或者Wireshark分析消息的交互。
2. 之后通过requests模拟。
由于没有具体的网址,我没有办法给出具体的代码。
给公众号发送特定的问题:
1. 建议使用Python的微信API(pip install itchat)
2. 由于没有提供Python版本,我这里给出兼容的解决方案
#coding=utf8
import threading
import itchat
SIGN_IN_MP_DICT = {
u'学校微信公众号': u'学校签到口令',
u'公司微信公众号': u'公司签到口令', }
def get_day(timeGap):
return int(time.strftime('%y%m%d', time.localtime(time.time() + timeGap)))
NEXT_SIGN_DATE = get_day(60*60*24)
def sign_in_thread():
''' 签到线程
如果尚未到需要签到的日期,则继续循环
如果到了需要签到的日期,则完成两个公众号的签到,并更新日期
'''
while 1:
if get_day <NEXT_SIGN_DATE:
time.sleep(30)
else:
for k, v in SIGN_IN_MP_DICT.items():
itchat.send(msg=v,
toUserName=itchat.search_mps(name=k)[0]['UserName'])
NEXT_SIGN_DATE = get_day(60*60*24)
itchat.auto_login(True)
# 测试是否存在特定公众号
for mpName in SIGN_IN_MP_DICT.keys():
mpList = itchat.search_mps(name=mpName)
if len(mpList) != 1:
print(u'没有检测到公众号“%s”,请检查名称')
break
else:
signInThread = threading.Thread(target=sign_in_thread)
signInThread.setDaemon(True)
signInThread.start()
itchat.run()
最近一段时间想看看能不能用万能的python来对微信进行一些操作(比如自动抢红包之类的...hahahaha),所以就在此记录一下啦~~
1、安装
sudo pip install itchat
2、登录
itchat.auto_login()
注:itchat.auto_login()这种方法将会通过微信扫描二维码登录,但是这种登录的方式确实短时间的登录,并不会保留登录的状态,也就是下次登录时还是需要扫描二维码,如果加上hotReload==True,那么就会保留登录的状态,至少在后面的几次登录过程中不会再次扫描二维码,该参数生成一个静态文件itchat.pkl用于存储登录状态
itchat.auto_login(hotReload=True)
3、退出登录
主要使用的是回调函数的方法,登录完成后的方法需要赋值在 loginCallback中退出后的方法,需要赋值在 exitCallback中.若不设置 loginCallback的值, 将会自动删除二维码图片并清空命令行显示.
import itchat,time
def lcb():
print("登录完成!")
def ecb():
print("退出成功!")
itchat.auto_login(loginCallback=lcb,exitCallback=ecb) #源码中规定需要用回调函数。
time.sleep(10)
itchat.logout() #强制退出登录
4、发送消息
send()
itchat.send(msg="WANGPC的微信消息!",toUserName="filehelper") #返回值为True或Flase
实例:
或者:
send_msg
send_msg(msg='Text Message', toUserName=None),其中的的msg是要发送的文本,toUserName是发送对象, 如果留空, 将发送给自己,返回值为True或者False
实例代码
send_file
send_file(fileDir, toUserName=None) fileDir是文件路径, 当文件不存在时, 将打印无此文件的提醒,返回值为True或者False
实例代码
send_image
send_image(fileDir, toUserName=None) 参数同上
实例代码
send_video
send_video(fileDir, toUserName=None) 参数同上
实例代码
创建步骤:
1.申请免费且支持python的服务器,新浪云sae,新建SAE应用之后,有两种代码提交方式,建议使用SVN(因为git支持代码提交,但不支持环境配置);
2.将对应版本的信息复制到微信开发-基本配置-URL,提交显示错误,因为还没有写代码,可以先用web框webpy架写个网页;
查看webpy使用说明:http://www.webpy.org/install.zh-cn
查看ase进行python开发入门说明:http://www.sinacloud.com/doc/sae/python/index.html
3.配置信息,告诉新浪云需要什么运行环境。点击代码管理-编辑代码,将用到的第三方库信息写入config.yaml,注意破折号,冒号后面空格!!
libraries:- name: webpy
version: "0.36"
- name: lxml
version: "2.3.4"
在index.wsgi文件中写入python启动程序
新建文件,写入接受微信get请求验证的Python文件
4.在index.wgsi中写入以下信息:
#coding=utf-8import os
import sae
import web
from weixinInterface import WeixinInterface
#配置web的路由
urls = (
'/weixin','WeixinInterface'
)
#拼接路径
app_root=os.path.dirname(__file__)
templates_root = os.path.join(app_root,'templates')
#渲染模版
render = web.template.render(templates_root)
#启动app
app = web.application(urls,globals()).wsgifunc()
application = sae.create_wsgi_app(app)
5.在自己编写的Python文件中写入微信验证和接受信息的程序
#coding=utf-8import hashlib
import web
import time
import os
from lxml import etree
#hashlib用于加密,md5,hash等
#lxml用来解析xml文件
class WeixinInterface(object):
#初始化
def __init__(self):
#拼接路径
self.app_root = os.path.dirname(__file__)
self.templates_root = os.path.join(self.app_root,'templates')
#渲染模版
self.render = web.template.render(self.templates_root)
#使用get方法,接收微信的get请求,看开发者文档的说明
#http://mp.weixin.qq.com/wiki/8/f9a0b8382e0b77d87b3bcc1ce6fbc104.html
def GET(self):
data = web.input()
signature = data.signature#微信加密签名
timestamp = data.timestamp#时间戳
nonce = data.nonce#随机数
echostr = data.echostr#随即字符串
token = 'zq90857'#自己设置的token
#将token、timestamp、nonce三个参数进行字典序排序
list = [token,timestamp,nonce]
list.sort()
#将三个参数字符串拼接成一个字符串进行sha1加密
sha1=hashlib.sha1()
map(sha1.update,list)
temStr = sha1.hexdigest()#加密
#判断
if temStr == signature:
return echostr
6.假设接收文字信息,按照开发者文档的要求,配置template文件夹下reply_text.xml文件
$def with(toUser,fromUser,createtime,content)<xml>
<ToUserName><![CDATA[$toUser]]></ToUserName>
<FromUserName><![CDATA[$fromUser]]></FromUserName>
<CreateTime>$createtime</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[$content]]></Content>
</xml>