转载注明原文地址: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访问mysql方法数据库

    1.下载界面包mysql-connector-java-5.0.8-bin.jar 下载链接 2.程序 (1)载入驱动 (2)编程连接操作 (3)返回结果处理 编程演示样例 import java.s ...

  2. bzoj1497(最小割)

    传送门:最大获利 题意:建立n个中转站,每个花费P[i],有m个用户,使用Ai和Bi中转站可获利Ci,问最终建立哪几个中转站使获利最大? 分析:根据最大权闭合图建图,用户群和中转站为带权的点集,用户群 ...

  3. ASP.NET MVC的跳转攻击问题

    在ASP.NET MVC的自带的模板代码中,有这样一段,用来拦截非登录用户,使其跳转到登录页面,然后登录后在跳转回原页面.所以,期间有一个returnUrl参数用来保存原页面地址.在Login Act ...

  4. hdu1690 Bus System (dijkstra)

    Problem Description Because of the huge population of China, public transportation is very important ...

  5. 将svnkit转成dlls时的问题

    未处理 System.TypeInitializationException Message="“org.tmatesoft.svn.core.internal.wc.DefaultSVNO ...

  6. CSS selectors for Selenium with example,selenium IDE

    CSS selectors for Selenium with example http://seleniumeasy.com/selenium-tutorials/css-selectors-tut ...

  7. JAVA 根据经纬度算出附近的正方形的四个角的经纬度

    /** * * @param longitude 经度 * @param latitude 纬度 * @param distance 范围(米) * @return */ public static ...

  8. 【Android Training - UserInfo】记住登入用户的信息[Lesson 1 - 使用AccountManager来记住用户]

    Remembering Your User[记住你的用户] 每一个人都非常喜欢自己的名字能被人记住.当中最简单,最有效的使得你的app让人喜欢的方法是记住你的用户是谁,特别是当用户升级到一台新的设备或 ...

  9. ThinkPHP实现用户登录

    ThinkPHP实现用户登录 ? //默认url入口 ? class IndexAction extends Action{ function index() { //检查用户是否登录 if(isse ...

  10. java 线程 新类库中的构件 countDownLatch 使用

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlhbmdydWkxOTg4/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...