python 获取微信好友列表的方法(微信web)


Posted in Python onFebruary 21, 2019

如下所示:

import urllib
import urllib2 
import os
import time
import re 
import cookielib 
import xml.dom.minidom 
import json
 
tip = 0 
uuid = ''
successUrl = ''
skey = ''
wxsid = ''
wxuin = ''
pass_ticket = ''
deviceId = 'e000000000000000'
imagesPath = os.getcwd() + '/weixin.jpg'
 
BaseRequest = {}
base_uri = '' 
push_uri = ''
 
def getUUID():
  global uuid
  url = 'https://login.weixin.qq.com/jslogin'
  values = { 
    'appid':'wx782c26e4c19acffb',
    'redirect_uri':'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage',
    'fun':'new',
    'lang':'zh_CN',
    '_':int(time.time())
  } 
  request = urllib2.Request(url=url, data=urllib.urlencode(values))  
  response = urllib2.urlopen(request)
  data = response.read() 
  print data 
   
  regx = r'window.QRLogin.code = (\d+); window.QRLogin.uuid = "(\S+?)"' 
  pm = re.search(regx, data) 
  code = pm.group(1) 
  uuid = pm.group(2) 
  print code, uuid 
   
  if code == '200': 
    return True 
  return False 
 
def show2DimensionCode(): 
  global tip
 
  url = 'https://login.weixin.qq.com/qrcode/' + uuid 
  values = { 
    't':'webwx',
    '_':int(time.time()) 
  } 
 
  request = urllib2.Request(url=url, data=urllib.urlencode(values)) 
  response = urllib2.urlopen(request) 
  tip = 1 
 
  f = open(imagesPath, 'wb') 
  f.write(response.read()) 
  f.close() 
  time.sleep(1)
  os.system('call %s' % imagesPath) 
  print u'please sacn qcode by your phone'.encode('GBK') 
  
def isLoginSucess():
  global successUrl, base_uri, push_uri
  
  url = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip=%s&uuid=%s&_=%s' % (tip, uuid, int(time.time())) 
  request = urllib2.Request(url=url) 
  response = urllib2.urlopen(request) 
  data = response.read() 
  print data 
  regx = r'window.code=(\d+)'
  regxLogin = r'window.redirect_uri="(\S+?)"' 
  pm = re.search(regx, data) 
  pmLogin = re.search(regxLogin, data)
  code = pm.group(1)
  if pmLogin != None:
   successUrl = pmLogin.group(1) + '&fun=new&version=v2'
 
  if code == '201': 
    print'Scan QR code successfully!' 
  elif code == '200': 
    print'Logining...' 
    services = [ 
     ('wx2.qq.com', 'webpush2.weixin.qq.com'), 
     ('qq.com', 'webpush.weixin.qq.com'), 
     ('web1.wechat.com', 'webpush1.wechat.com'), 
     ('web2.wechat.com', 'webpush2.wechat.com'), 
     ('wechat.com', 'webpush.wechat.com'), 
     ('web1.wechatapp.com', 'webpush1.wechatapp.com'), 
    ] 
    base_uri = successUrl[:successUrl.rfind('/')] 
    push_uri = base_uri 
    for (searchUrl, pushUrl) in services: 
     if base_uri.find(searchUrl) >= 0: 
      push_uri = 'https://%s/cgi-bin/mmwebwx-bin' % pushUrl 
      break 
  elif code == '408': 
    print'Login Timeout!'
 
  return code  
 
def webwxnewloginpage():
 global successUrl, skey, wxsid, wxuin, pass_ticket, BaseRequest
 
 request = urllib2.Request(url=successUrl) 
 response = urllib2.urlopen(request) 
 data = response.read()
 
 doc = xml.dom.minidom.parseString(data) 
 root = doc.documentElement 
 
 for node in root.childNodes: 
  if node.nodeName == 'skey': 
   skey = node.childNodes[0].data 
  elif node.nodeName == 'wxsid': 
   wxsid = node.childNodes[0].data 
  elif node.nodeName == 'wxuin': 
   wxuin = node.childNodes[0].data 
  elif node.nodeName == 'pass_ticket': 
   pass_ticket = node.childNodes[0].data
   
 BaseRequest = { 
  'Uin': wxuin, 
  'Sid': wxsid, 
  'Skey': skey, 
  'DeviceID': deviceId, 
 }
 
def webwxinit(): 
 global skey, pass_ticket, BaseRequest, base_uri
 
 url = (base_uri + '/webwxinit?pass_ticket=%s&skey=%s&r=%s' % (pass_ticket, skey, int(time.time()))) 
 params = {'BaseRequest': BaseRequest} 
 headers = {'content-type': 'application/json; charset=UTF-8'}
 request = urllib2.Request(url=url, data=json.dumps(params), headers=headers) 
 response = urllib2.urlopen(request) 
 data = response.read()
 print data
 
def webwxgetcontact(): 
 global skey, pass_ticket, base_uri
  
 url = (base_uri + '/webwxgetcontact?pass_ticket=%s&skey=%s&r=%s' % (pass_ticket, skey, int(time.time()))) 
 headers = {'content-type': 'application/json; charset=UTF-8'}
 request = urllib2.Request(url=url, headers=headers) 
 response = urllib2.urlopen(request)
 data = response.read()
 print data
 
def main(): 
 
  cookie = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar())) 
  urllib2.install_opener(cookie) 
 
  if getUUID() == False: 
   print'Get uuid unsuccessfully!' 
   return None 
 
  show2DimensionCode() 
  time.sleep(1) 
 
  while isLoginSucess() != '200': 
   pass 
 
  webwxnewloginpage()
#   time.sleep(1)
#   webwxinit()
  time.sleep(1)
  webwxgetcontact()
  
  os.remove(imagesPath) 
  print'Login successfully!' 
 
if __name__ == '__main__': 
  print'Welcome to use weixin personnal version' 
  print'Please click Enter key to continue......'
  main()

以上这篇python 获取微信好友列表的方法(微信web)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python 正则表达式 概述及常用字符
May 04 Python
python使用wxPython打开并播放wav文件的方法
Apr 24 Python
python实现的DES加密算法和3DES加密算法实例
Jun 03 Python
Python爬虫抓取手机APP的传输数据
Jan 22 Python
Python中shutil模块的学习笔记教程
Apr 04 Python
详解python中executemany和序列的使用方法
Aug 12 Python
Python 关于反射和类的特殊成员方法
Sep 14 Python
django使用html模板减少代码代码解析
Dec 12 Python
浅谈Python中的私有变量
Feb 28 Python
使用Python实现企业微信的自动打卡功能
Apr 30 Python
python实现LRU热点缓存及原理
Oct 29 Python
基于python2.7实现图形密码生成器的实例代码
Nov 05 Python
Python基于mysql实现学生管理系统
Feb 21 #Python
python+mysql实现教务管理系统
Feb 20 #Python
python Tkinter版学生管理系统
Feb 20 #Python
啥是佩奇?使用Python自动绘画小猪佩奇的代码实例
Feb 20 #Python
Python实战购物车项目的实现参考
Feb 20 #Python
利用django+wechat-python-sdk 创建微信服务器接入的方法
Feb 20 #Python
python3+pyqt5+itchat微信定时发送消息的方法
Feb 20 #Python
You might like
php实现utf-8和GB2312编码相互转换函数代码
2013/02/07 PHP
Laravel Reponse响应客户端示例详解
2020/09/03 PHP
图像替换新技术 状态域方法
2010/01/28 Javascript
jQuery图片播放8款精美插件分享
2013/02/17 Javascript
jquery给图片添加鼠标经过时的边框效果
2013/11/12 Javascript
JavaScript怎么判断图片是否加载完成以便获取其尺寸
2014/05/08 Javascript
jquery图片切换实例分析
2015/04/15 Javascript
Nginx上传文件全部缓存解决方案
2015/08/17 Javascript
老生常谈JavaScript数组的用法
2016/06/10 Javascript
jQuery实现微信长按识别二维码功能
2016/08/26 Javascript
原生js实现放大镜效果
2017/01/11 Javascript
详解Angular.js数据绑定时自动转义html标签及内容
2017/03/30 Javascript
Express框架之connect-flash详解
2017/05/31 Javascript
angularjs $http实现form表单提交示例
2017/06/09 Javascript
AngularJS中scope的绑定策略实例分析
2017/10/30 Javascript
vue 项目打包通过命令修改 vue-router 模式 修改 API 接口前缀
2018/06/13 Javascript
vue+iview+less 实现换肤功能
2018/08/17 Javascript
vue实现的下拉框功能示例
2019/01/29 Javascript
jquery简单实现纵向的无缝滚动代码实例
2019/04/01 jQuery
js基础之事件捕获与冒泡原理
2019/10/09 Javascript
vue之组件内监控$store中定义变量的变化详解
2019/11/08 Javascript
小程序中使用css var变量(使js可以动态设置css样式属性)
2020/03/31 Javascript
我所理解的JavaScript中的this指向
2020/09/04 Javascript
利用pyecharts实现地图可视化的例子
2019/08/12 Python
python 通过视频url获取视频的宽高方式
2019/12/10 Python
Python多线程实现支付模拟请求过程解析
2020/04/21 Python
Python多线程的退出控制实现
2020/08/10 Python
python中not、and和or的优先级与详细用法介绍
2020/11/03 Python
Pretty You London官网:英国拖鞋和睡衣品牌
2019/05/08 全球购物
Ego Shoes官网:英国时髦鞋类品牌
2020/10/19 全球购物
幼儿教育感言
2014/02/05 职场文书
授权委托书范文
2014/07/31 职场文书
python代码实现备忘录案例讲解
2021/07/26 Python
Pandas实现DataFrame的简单运算、统计与排序
2022/03/31 Python
Win11 22H2 2022怎么更新? 获得Win1122H22022版本升级技巧
2022/09/23 数码科技
html解决浏览器记住密码输入框的问题
2023/05/07 HTML / CSS