转载注明原文地址: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. hdu 3309 Roll The Cube ( bfs )

    Roll The Cube Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  2. 我在开发网站后得出的asp.Net网站优化总结

    原文:我在开发网站后得出的asp.Net网站优化总结 asp.Net网站优化总结: 1.数据库优化: 首先是对查询语句的优化,开发时为了图快可以不考虑,但是后期维护时就必须要优化(比如:你调用代码生成 ...

  3. 使用CXF创建REST WEBSERVICE

    简单小结下CXF跟REST搭配webservice的做法,直接举代码为样例: 1 order.java   package com.example.rest; import javax.xml.bin ...

  4. asp.net出现正在中止线程解决方案

    刚才又再次遇到了一个之前遇到的问题,在这里记录一下. 起因: 如果使用 Response.End.Response.Redirect 或 Server.Transfer 方法,将出现 ThreadAb ...

  5. AngularJS是为了克服HTML在构建应用上的不足而设计的

    AngularJS中文网:http://www.apjs.net/ 简介   AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构 ...

  6. web开发性能优化---项目架构篇

    项目技术架构层级规划和介绍 简称四横两纵 四横即四大层次.分别为: 1.用户渠道层:用户渠道层是直接面向终于用户.通过站点的形式向用户提供产品展示.企业市场宣传.对产品的订购.互动分享.客户关怀以及用 ...

  7. oracle ebs 12.20 安装成功其过程失败日记及总结(1)

    由于公司业务须要,须要安装oracle ebs进行 form 开发,所以就開始了痛苦oracle ebs安装之过程.刚開始是在vm中win2003 server 中安装ebs,,不知是我自已的水平太差 ...

  8. Knockout应用开发指南 第四章:模板绑定

    原文:Knockout应用开发指南 第四章:模板绑定 模板绑定The template binding 目的 template绑定通过模板将数据render到页面.模板绑定对于构建嵌套结构的页面非常方 ...

  9. 用XAML做网页!!—页头

    原文:用XAML做网页!!-页头 接续上次进度,我们此次来制作页头. 首先要实现两侧边缘的美化,如下图所示: 在边缘处有一层朦胧的亮度反光效果,这也是通过简单的渐变实现的,而且我们在后面的每个区块中都 ...

  10. (40)JS运动之右下角悬浮框

    <!DOCTYPE HTML> <!-- --> <html> <head> <meta charset="utf-8"> ...