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学习笔记 下载
Feb 10 Python
Windows下使Python2.x版本的解释器与3.x共存的方法
Oct 25 Python
python中pygame针对游戏窗口的显示方法实例分析(附源码)
Nov 11 Python
使用rst2pdf实现将sphinx生成PDF
Jun 07 Python
使用python 3实现发送邮件功能
Jun 15 Python
python 递归深度优先搜索与广度优先搜索算法模拟实现
Oct 22 Python
Django 404、500页面全局配置知识点详解
Mar 10 Python
Pycharm学生免费专业版安装教程的方法步骤
Sep 24 Python
Python中常用的os操作汇总
Nov 05 Python
python 利用jieba.analyse进行 关键词提取
Dec 17 Python
一行代码python实现文件共享服务器
Apr 22 Python
Python实现生活常识解答机器人
Jun 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中读写文件与读写数据库的效率比较分享
2013/10/19 PHP
php 如何获取文件的后缀名
2016/06/05 PHP
CI框架中类的自动加载问题分析
2016/11/21 PHP
PHP仿tp实现mvc框架基本设计思路与实现方法分析
2018/05/23 PHP
PHP的mysqli_rollback()函数讲解
2019/01/23 PHP
php菜单/评论数据递归分级算法的实现方法
2019/08/01 PHP
php写app用的框架整理
2019/09/29 PHP
获取页面高度,窗口高度,滚动条高度等参数值getPageSize,getPageScroll
2006/09/22 Javascript
50 个 jQuery 插件可将你的网站带到另外一个高度
2016/04/26 Javascript
jQuery EasyUI 入门必看
2016/06/03 Javascript
移动开发之自适应手机屏幕宽度
2016/11/23 Javascript
javascript实现一个网页加载进度loading
2017/01/04 Javascript
AngularJS service之select下拉菜单效果
2017/07/28 Javascript
详解使用angular的HttpClient搭配rxjs
2017/09/01 Javascript
微信小程序wepy框架笔记小结
2018/08/08 Javascript
如何使用less实现随机下雪动画详解
2019/01/02 Javascript
微信小程序实现树莓派(raspberry pi)小车控制
2020/02/12 Javascript
Vue插件之滑动验证码用法详解
2020/04/05 Javascript
Vue 监听元素前后变化值实例
2020/07/29 Javascript
vue添加锚点,实现滚动页面时锚点添加相应的class操作
2020/08/10 Javascript
layui使用及简单的三级联动实现教程
2020/12/01 Javascript
[06:25]DOTA2英雄梦之声_第17期_大地之灵
2014/06/20 DOTA
[01:15:56]2018DOTA2亚洲邀请赛3月30日 小组赛A组 TNC VS Newbee
2018/03/31 DOTA
Python3使用xml.dom.minidom和xml.etree模块儿解析xml文件封装函数的方法
2019/09/23 Python
Python利用逻辑回归模型解决MNIST手写数字识别问题详解
2020/01/14 Python
PIL包中Image模块的convert()函数的具体使用
2020/02/26 Python
Python 如何测试文件是否存在
2020/07/31 Python
利用纯CSS3实现动态的自行车特效源码
2017/01/20 HTML / CSS
购买限量版收藏品、珠宝和礼品:Bradford Exchange
2016/09/23 全球购物
马歇尔耳机官网:Marshall Headphones
2020/02/04 全球购物
个人四风问题整改措施思想汇报
2014/10/04 职场文书
学生党员批评与自我批评
2014/10/15 职场文书
退休欢送会致辞
2015/07/31 职场文书
2016教师校本研修心得体会
2016/01/08 职场文书
小米11和iphone12哪个值得买?小米11对比iphone12评测
2021/04/21 数码科技
mysql字段为NULL索引是否会失效实例详解
2022/05/30 MySQL