python获取的信息列表微信公共平台和用户头像
转载注明原文地址:http://blog.csdn.net/btyh17mxy/article/details/25207889
只写模拟登陆的方式获取微信从信息和头像库列表公共平台, - 相关后,功能将继续增加。github地址https://github.com/btyh17mxy/wxwall
#!/usr/bin/env python
# coding: UTF-8
import json
import hashlib
import re
import random
import json
import requests
import logging
DEBUG_LEVEL = logging.DEBUG
try:
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):
pass class 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("微信公众平台账户名","password")
weixin.get_msg_list()
weixin.get_user_icon()
这里加入一个測试连接http://mushapi.sinaapp.com/new-blog-online.html
版权声明:本文博客原创文章,博客,未经同意,不得转载。
python获取的信息列表微信公共平台和用户头像的更多相关文章
- PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息限制
2013年10月06日最新整理. PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息限制 微信公共平台消息主动推送接口一直是腾讯的私用接口,相信很多朋友都非常想要用到这个功能. 通过学习 ...
- ASP.NET MVC 微信公共平台开发之获取用户消息并处理
ASP.NET MVC 微信公共平台开发 获取用户消息并处理 获取用户消息 用户发送的消息是在微信服务器发送的一个HTTP POST请求中包含的,获取用户发送的消息要从POST请求的数据流中获取 微信 ...
- ASP.NET MVC 微信公共平台开发之验证消息的真实性
ASP.NET MVC 微信公共平台开发 验证消息的真实性 在MVC Controller所在项目中添加过滤器,在过滤器中重写 public override void OnActionExecuti ...
- ASP.NET MVC 微信公共平台开发之 微信接入
ASP.NET MVC 接入微信公共平台 申请微信公共账号 既然要接入微信公共平台,微信公共号是必须的(当然如果只是测试的话也可以申请微信公共平台接口测试账号),来这里微信公共平台 申请微信公共号(注 ...
- C#开发BIMFACE系列7 服务端API之获取文件信息列表
系列目录 [已更新最新开发文章,点击查看详细] 本文详细介绍如何获取BIMFACE平台中所有上传过的文件信息列表. 请求地址:GET https://file.bimface.com/file ...
- 微信公共平台注册 bug: 验证码不应该输入后,就立即检查其有效性
本文链接: https://www.cnblogs.com/hchengmx/p/10793037.html 刚刚想注册个微信公众号,就发现了这个问题,在这里记录一下. 已经发到testhome了,链 ...
- 合肥 专业做APP(安卓,ios) 微信公共平台
合肥 专业做APP(安卓,ios) 微信公共平台 电话:15715696592
- 微信公共平台开发-(.net实现)1--成为开发者
刚换了个新环境,哎这都快一个月了,还没适应过来,还是怀念老地方呀.老板让开发一个基于微信平台的开发项目,而且是用net实现.当时就蒙了,微信就用了一会个人赶脚不好,所以果断不用,现在让开发,而且查了一 ...
- 微信公共平台开发1 .net
如果想通过微信去开发(当然,指的是开发模式下),首先 建议先申请一个服务号,因为服务号的功能与接口也多,有些功能订阅号是实现不了的.另外申请过以后必须得通过微信 认证才能开发,好像是得付300大洋,高 ...
随机推荐
- Git使用之搭建基于SSH的Gitserver(上篇)
1. 须要软件 msysgit (Gitfor Windows) Copssh (OpenSSHfor Windows,新版本号已经開始收费了大家能够去搜索引擎找曾经的免费版Copssh_4.1.0下 ...
- Python处理海量手机号码
Python处理海量手机号码 一.任务描述 上周,老板给我一个小任务:批量生成手机号码并去重.给了我一个Excel表,里面是中国移动各个地区的可用手机号码前7位(如下图),里面有十三张表,每个表里的电 ...
- 通用型CRM还是行业型CRM?-定制为王
大数据时代,怎样利用工具摆脱繁杂的数据管理之苦,洞察有价值的销售信息,是每一个管理者的迫切须要.Zoho CRM问世10年来,见证了一个个行业客户怎样在CRM帮助下实现了效率和业绩提升.相同,广泛的 ...
- poj 1011 Sticks ,剪枝神题
木棒 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 118943 Accepted: 27429 Description 乔治拿 ...
- 西南民大oj(两园交求面积)
西南民大oj:http://www.swunacm.com/acmhome/welcome.do?method=index 我的几何不可能那么可爱 时间限制(普通/Java) : 1000 MS/ 3 ...
- 单片机实验: 三轴磁场模块 GY-271
最近买了一块三轴磁场模块进行实验 名称:HMC5883L模块(三轴磁场模块) 型号:GY-271 使用芯片:HMC5883L 供电电源:3-5v 通信方式:IIC通信协议 测量范围:±1.3-8 高斯 ...
- Net分布式系统
Net分布式系统 Net分布式系统之三:Keepalived+LVS+Nginx负载均衡之高可用 摘要: 上一篇写了nginx负载均衡,此篇实现高可用(HA).系统整体设计是采用Nginx做负载均衡, ...
- Knockout应用开发指南 第三章:绑定语法(2)
原文:Knockout应用开发指南 第三章:绑定语法(2) 7 click 绑定 目的 click绑定在DOM元素上添加事件句柄以便元素被点击的时候执行定义的JavaScript 函数.大部分是用 ...
- linux crontab定时执行shell脚本
linux下使用crontab命令被用来提交和管理用户的需要周期性执行的任务,示例如下:crontab -e 编辑周期任务30 21 * * * /etc/init.d/smb restart 每晚的 ...
- iOS随机颜色
#import <UIKit/UIKit.h> @interface UIColor (RandomColor) +(UIColor *) randomColor; @end #impor ...