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实现用户登陆邮件通知的方法
Jul 09 Python
深入解析Python中的上下文管理器
Jun 28 Python
python检查URL是否正常访问的小技巧
Feb 25 Python
python随机取list中的元素方法
Apr 08 Python
在双python下设置python3为默认的方法
Oct 31 Python
python实现简单加密解密机制
Mar 19 Python
使用python分析统计自己微信朋友的信息
Jul 19 Python
python pandas 时间日期的处理实现
Jul 30 Python
django 实现后台从富文本提取纯文本
Jul 02 Python
面向新手解析python Beautiful Soup基本用法
Jul 11 Python
python 深度学习中的4种激活函数
Sep 18 Python
分享3个非常实用的 Python 模块
Mar 03 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
用C/C++扩展你的PHP 为你的php增加功能
2012/09/06 PHP
php中的注释、变量、数组、常量、函数应用介绍
2012/11/16 PHP
php多种形式发送邮件(mail qmail邮件系统 phpmailer类)
2014/01/22 PHP
jQuery+Ajax+PHP“喜欢”评级功能实现代码
2015/10/08 PHP
详解php中 === 的使用
2016/10/24 PHP
Linux平台php命令行程序处理管道数据的方法
2016/11/10 PHP
详解php中curl返回false的解决办法
2019/03/18 PHP
javascript应用:Iframe自适应其加载的内容高度
2007/04/10 Javascript
JavaScript 内置对象属性及方法集合
2010/07/04 Javascript
js常用代码段整理
2011/11/30 Javascript
关于JS中的闭包浅谈
2013/08/23 Javascript
浅析IE10兼容性问题(frameset的cols属性)
2014/01/03 Javascript
thinkphp实现无限分类(使用递归)
2015/12/19 Javascript
jQuery实现简单的抽奖游戏
2017/05/05 jQuery
bootstrap table sum总数量统计实现方法
2017/10/29 Javascript
详解Next.js页面渲染的优化方案
2019/01/27 Javascript
element-ui 本地化使用教程详解
2019/10/28 Javascript
vue 重塑数组之修改数组指定index的值操作
2020/08/09 Javascript
python3.3使用tkinter开发猜数字游戏示例
2014/03/14 Python
python简单实现旋转图片的方法
2015/05/30 Python
Python 记录日志的灵活性和可配置性介绍
2018/02/27 Python
Python装饰器用法实例总结
2018/05/26 Python
Linux CentOS Python开发环境搭建教程
2018/11/28 Python
Python HTMLTestRunner可视化报告实现过程解析
2020/04/10 Python
python3+opencv 使用灰度直方图来判断图片的亮暗操作
2020/06/02 Python
python中rb含义理解
2020/06/18 Python
雅诗兰黛旗下专业男士保养领导品牌:Lab Series
2017/05/15 全球购物
单位实习证明怎么写
2014/01/17 职场文书
公益广告宣传方案
2014/02/28 职场文书
公司廉洁自律承诺书
2014/03/27 职场文书
课外科技活动总结
2014/08/27 职场文书
新郎结婚保证书
2015/02/26 职场文书
优秀大学生自荐信
2015/03/26 职场文书
2015年大学生工作总结
2015/04/21 职场文书
高二英语教学反思
2016/03/03 职场文书
苹果可能正在打击不进行更新的 App
2022/04/24 数码科技