转载注明原文地址: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获取的信息列表微信公共平台和用户头像的更多相关文章

  1. PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息限制

    2013年10月06日最新整理. PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息限制 微信公共平台消息主动推送接口一直是腾讯的私用接口,相信很多朋友都非常想要用到这个功能. 通过学习 ...

  2. ASP.NET MVC 微信公共平台开发之获取用户消息并处理

    ASP.NET MVC 微信公共平台开发 获取用户消息并处理 获取用户消息 用户发送的消息是在微信服务器发送的一个HTTP POST请求中包含的,获取用户发送的消息要从POST请求的数据流中获取 微信 ...

  3. ASP.NET MVC 微信公共平台开发之验证消息的真实性

    ASP.NET MVC 微信公共平台开发 验证消息的真实性 在MVC Controller所在项目中添加过滤器,在过滤器中重写 public override void OnActionExecuti ...

  4. ASP.NET MVC 微信公共平台开发之 微信接入

    ASP.NET MVC 接入微信公共平台 申请微信公共账号 既然要接入微信公共平台,微信公共号是必须的(当然如果只是测试的话也可以申请微信公共平台接口测试账号),来这里微信公共平台 申请微信公共号(注 ...

  5. C#开发BIMFACE系列7 服务端API之获取文件信息列表

    系列目录     [已更新最新开发文章,点击查看详细] 本文详细介绍如何获取BIMFACE平台中所有上传过的文件信息列表. 请求地址:GET https://file.bimface.com/file ...

  6. 微信公共平台注册 bug: 验证码不应该输入后,就立即检查其有效性

    本文链接: https://www.cnblogs.com/hchengmx/p/10793037.html 刚刚想注册个微信公众号,就发现了这个问题,在这里记录一下. 已经发到testhome了,链 ...

  7. 合肥 专业做APP(安卓,ios) 微信公共平台

    合肥 专业做APP(安卓,ios) 微信公共平台 电话:15715696592

  8. 微信公共平台开发-(.net实现)1--成为开发者

    刚换了个新环境,哎这都快一个月了,还没适应过来,还是怀念老地方呀.老板让开发一个基于微信平台的开发项目,而且是用net实现.当时就蒙了,微信就用了一会个人赶脚不好,所以果断不用,现在让开发,而且查了一 ...

  9. 微信公共平台开发1 .net

    如果想通过微信去开发(当然,指的是开发模式下),首先 建议先申请一个服务号,因为服务号的功能与接口也多,有些功能订阅号是实现不了的.另外申请过以后必须得通过微信 认证才能开发,好像是得付300大洋,高 ...

随机推荐

  1. “>>”和“>>>” java

    “>>”算术右移运算符, 表示带符号右移,它使用最高位填充移位后左侧的空位.右移的结果为:每移一位,第一个操作数被2除一次,移动的次数由第二个操作数确定.按二进制形式把所有的数字向右移动对 ...

  2. 为什么 as sysdba着陆方法oracle数据库,为什么刚刚输入username和password我们都可以登录?

    事实上,这是oracle问题数据库的身份验证方法 该 sqlnet.ora在文件 SQLNET.AUTHENTICATION_SERVICES= (NTS) 变 SQLNET.AUTHENTICATI ...

  3. Centos 5.5 更新网卡驱动 bnx2 version: 2.0.2

    操作系统:CentOS release 5.5 (Final) 故障现象:网卡无故自动down掉,使用service  network restart 重启后没多久又会自动down , 连接数大概在2 ...

  4. poj3126(bfs)

    题目链接:http://poj.org/problem?id=3126 题意:给两个四位数n,m,将n变成m需要多少步,要求每次只能改变n的某一位数,即改变后的数与改变前的数只有一位不同,且每次改变后 ...

  5. HYSBZ 2243(树链剖分)

    题目连接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28982#problem/D 题意:给定一棵有n个节点的无根树及点权和m个操作, ...

  6. leetcode - Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  7. jquery validation remote depends 验证触发条件

    背景: 1:简介页面的变化: 2:邮箱不能反复 改动页面,邮箱默认带出,但字段上带有remote验证.不能反复,焦点由邮箱输入框失去时.会默认触发remote验证. 介绍完成 watermark/2/ ...

  8. PHP实现冒泡排序、双向冒泡排序算法

    冒泡排序(Bubble Sort),是一种较简单的.稳定的排序算法.冒泡排序算法步骤:比较相邻的元素,如果第一个比第二个大,就交换他们两个的位置:对每对相邻的元素执行同样的操作,这样一趟下来,最后的元 ...

  9. maven中的java库

    /* *  *         <dependency>    <groupId>io.netty</groupId>    <artifactId>n ...

  10. 浅析JAVA设计模式之工厂模式(一)

    1 工厂模式简单介绍 工厂模式的定义:简单地说,用来实例化对象,取代new操作. 工厂模式专门负责将大量有共同接口的类实例化.工作模式能够动态决定将哪一个类实例化.不用先知道每次要实例化哪一个类. 工 ...