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之集合(set)
Sep 24 Python
python爬取51job中hr的邮箱
May 14 Python
有趣的python小程序分享
Dec 05 Python
python实现排序算法解析
Sep 08 Python
Python eval的常见错误封装及利用原理详解
Mar 26 Python
详解Python下载图片并保存本地的两种方式
May 15 Python
Python+OpenCV+pyQt5录制双目摄像头视频的实例
Jun 28 Python
Python实现的远程文件自动打包并下载功能示例
Jul 12 Python
PyQt5 界面显示无响应的实现
Mar 26 Python
python数据类型强制转换实例详解
Jun 22 Python
Python爬虫自动化获取华图和粉笔网站的错题(推荐)
Jan 08 Python
pytorch DataLoader的num_workers参数与设置大小详解
May 28 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中使用与Perl兼容的正则表达式
2006/11/26 PHP
PHP中strtotime函数使用方法分享
2012/01/10 PHP
php实现的双向队列类实例
2014/09/24 PHP
微信公众号开发之通过接口删除菜单
2017/02/20 PHP
php解决安全问题的方法实例
2019/09/19 PHP
php集成开发环境详解
2019/09/24 PHP
Javascript常考语句107条收集
2010/03/09 Javascript
JQuery拖拽元素改变大小尺寸实现代码
2012/12/10 Javascript
js网页中的(运行代码)功能实现思路
2013/02/04 Javascript
利用JS进行图片的切换即特效展示图片
2013/12/03 Javascript
Jquery在指定DIV加载HTML示例代码
2014/02/17 Javascript
原生js和jquery实现图片轮播特效
2015/04/23 Javascript
nodejs创建web服务器之hello world程序
2015/08/20 NodeJs
原生JS实现旋转木马式图片轮播插件
2016/04/25 Javascript
js+css3制作时钟特效
2016/10/16 Javascript
vue指令以及dom操作详解
2017/03/04 Javascript
JavaScript实现音乐自动切换和轮播
2017/11/05 Javascript
nodejs async异步常用函数总结(推荐)
2017/11/17 NodeJs
layui 上传图片 返回图片地址的方法
2019/09/26 Javascript
vue a标签点击实现赋值方式
2020/09/07 Javascript
详细介绍Python中的偏函数
2015/04/27 Python
Python中文字符串截取问题
2015/06/15 Python
通过Python来使用七牛云存储的方法详解
2015/08/07 Python
Python基本数据结构与用法详解【列表、元组、集合、字典】
2019/03/23 Python
pyqt5 实现工具栏文字图片同时显示
2019/06/13 Python
python字典排序的方法
2019/10/12 Python
Flask 上传自定义头像的实例详解
2020/01/09 Python
Python 面向对象静态方法、类方法、属性方法知识点小结
2020/03/09 Python
Python turtle库的画笔控制说明
2020/06/28 Python
激光脱毛、蓝光和护肤:Tria Beauty
2019/03/28 全球购物
编写用C语言实现的求n阶阶乘问题的递归算法
2014/10/21 面试题
国际贸易实训报告
2014/11/05 职场文书
大二学年个人总结
2015/03/03 职场文书
酒店销售经理岗位职责
2015/04/02 职场文书
红与黑读书笔记
2015/06/29 职场文书
投诉信范文
2015/07/02 职场文书