python获取微信公共平台消息列表和用户头像
来源:互联网 发布:单片机交通灯设计c 编辑:程序博客网 时间:2024/06/05 06:43
转载需注明原文地址:http://blog.csdn.net/btyh17mxy/article/details/25207889
刚写的模拟登陆的方式从获取微信公众平台消息列表和用户头像的库,之后还会继续增加相关功能,github地址https://github.com/btyh17mxy/wxwall
#!/usr/bin/env python# coding: UTF-8import jsonimport hashlibimport reimport randomimport jsonimport requestsimport loggingDEBUG_LEVEL = logging.DEBUGtry: import colorizing_stream_handler root = logging.getLogger() root.setLevel(DEBUG_LEVEL) root.addHandler(colorizing_stream_handler.ColorizingStreamHandler())except Exception, e: print 'can not import colorizing_stream_handler, using logging.StreamHandler()' root = logging.getLogger() root.setLevel(DEBUG_LEVEL) root.addHandler(logging.StreamHandler())'''base exception class.'''class WeixinPublicError(Exception): pass'''raise when cookies expired.'''class WeixinNeedLoginError(WeixinPublicError): pass'''rasie when unenable to login.'''class WeixinLoginError(WeixinPublicError): passclass WeixinPublic(object): def __init__(self, account, pwd, token = None, cookies = None, ifencodepwd = False): self.account = account if ifencodepwd: self.pwd = pwd else: self.pwd = hashlib.md5(pwd).hexdigest() self.wx_cookies = cookies self.lastmsgid = 0 self.token = token if self.token == None or self.wx_cookies == None: self.token = '' self.wx_cookies = '' self.login() '''login to weichat, get token and cookies. Raise: WeixinLoginError, when can not get token from respond. ''' def login(self): url = 'https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN' payload = { 'username' : self.account, 'imgcode' : '', 'f' : 'json', 'pwd' : self.pwd, } headers = { 'x-requested-with' : 'XMLHttpRequest', 'referer' : 'https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN', } r = requests.post(url, data = payload, headers = headers) logging.info('------login------') logging.debug("respond:\t%s"%r.text) s = re.search(r'token=(\d+)', r.text) if not s: logging.error('Login Error!!!') raise Exception("Login error.") self.token = int(s.group(1)) logging.debug('token:\t%d'%self.token) self.wx_cookies = '' for cookie in r.cookies: self.wx_cookies += cookie.name + '=' + cookie.value + ';' logging.debug('cookies:\t%s'%self.wx_cookies) logging.info('------end login------') '''get message list. raise: WeixinNeedLoginError, when need re-login. returns: messages in dict. ''' def get_msg_list(self): logging.info('------get_msg_list------') url = 'https://mp.weixin.qq.com/cgi-bin/message?t=message/list&token=%s&count=20&day=7'%self.token payload = { 't':'message/list', 'token':self.token, 'count':20, 'day':7, } headers = { 'x-requested-with' : 'XMLHttpRequest', 'referer' : 'https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN', 'cookie' : self.wx_cookies, } r = requests.get(url, data = payload, headers = headers) c = "".join(r.text.split()) s = re.search(r'list:\((.*)\).msg_item', c) if s == None: logging.error('need re-login') raise WeixinNeedLoginError('need re-login') else: msg_list = s.group(1) logging.debug('msg_list:\t%s'%msg_list) return msg_list logging.info('------end get_msg_list------') '''get user icon. Args: fakeid. uri, local uri to store this img. ''' def get_user_icon(self, fakeid = 1155750780, uri = ''): logging.info('------get_user_icon------') url = "https://mp.weixin.qq.com/misc/getheadimg" payload = { 'token':self.token, 'fakeid':fakeid, } headers = { 'Cookie':self.wx_cookies, } r = requests.get(url, params = payload, headers = headers) respond_headers = r.headers if 'content-type' in respond_headers.keys() and not respond_headers['content-type'] == 'image/jpeg': logging.error('download user icon error, need re-login.') raise WeixinNeedLoginError('download user icon error, need re-login.') if uri == '': f = open('%d.jpg'%(fakeid),'wb+') else: f = open('%s/%d.jpg'%(uri, fakeid),'wb+') f.write(r.content) f.close() logging.info('------end get_user_icon------')if __name__ == '__main__': weixin = WeixinPublic("微信公众平台账户名","密码") weixin.get_msg_list() weixin.get_user_icon()
这里添加一个测试连接http://mushapi.sinaapp.com/new-blog-online.html
3 0
- python获取微信公共平台消息列表和用户头像
- 微信公众平台 获取用户列表
- 微信公众平台获取用户头像信息
- python 微信公共平台
- php 微信模拟登陆 获取用户列表 群发消息
- php 微信模拟登陆 获取用户列表 群发消息
- 微信接入之获取用户头像
- 用python批量获取微信头像
- 微信开发公共服务平台开发(.Net) - 网页授权获取用户基本信息
- 微信登录,并获取用户头像等数据
- 微信公众账号调取用户昵称和用户头像
- 微信公共平台 首次关注 自动回复消息
- 浅析 微信公共平台消息 开发原理
- 微信公共平台开发(二):回复文本消息
- 爬虫-微信公众平台消息获取
- 微信公共平台
- PHP7.0微信公众平台开发5: 实例二:获取用户列表
- 微信公众号--获取用户列表
- 在Flex中判断是否在组件之外单击的技巧
- Enable Accelerated Compositing on the WebKit Android NDK port && Texture Mapper
- 在 Mac 中安装 MySQLdb (Python mysql )
- URL encoding
- org.apache.catalina.connector.Request.parseParameters(Request.java:2446) NullPointerException异常处理
- python获取微信公共平台消息列表和用户头像
- Animatepacker for cocos2d-x 3.0 解析
- qwt的安装(绝对正确)
- Tomcat8 配置 c/c++ 的cgi
- 物联网厂家---慧感嘉联简介
- Linux C 编程内存泄露检测工具(一):mtrace
- DB2 数据库创建、IXF格式 表的导出导入。
- zoj 3777 Problem Arrangement(壮压+背包)
- Mac OS X 10.9.3 Beta 8升级教程