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中映射类型(字典)操作符的概念和使用
Aug 19 Python
详解Python操作RabbitMQ服务器消息队列的远程结果返回
Jun 30 Python
详解python中xlrd包的安装与处理Excel表格
Dec 16 Python
python实现K近邻回归,采用等权重和不等权重的方法
Jan 23 Python
Python面向对象进阶学习
May 21 Python
python使用 zip 同时迭代多个序列示例
Jul 06 Python
Python pandas实现excel工作表合并功能详解
Aug 29 Python
python的pyecharts绘制各种图表详细(附代码)
Nov 11 Python
查看端口并杀进程python脚本代码
Dec 17 Python
Django REST framwork的权限验证实例
Apr 02 Python
Django xadmin安装及使用详解
Oct 26 Python
golang特有程序结构入门教程
Jun 02 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
为什么《星际争霸》是测试人工智能的理想战场
2019/12/03 星际争霸
ThinkPHP3.1数据CURD操作快速入门
2014/06/19 PHP
PHP正则表达式过滤html标签属性(DEMO)
2016/05/04 PHP
php导出csv文件,可导出前导0实例代码
2016/11/16 PHP
JavaScript URL参数读取改进版
2009/01/16 Javascript
在js文件中如何获取basePath处理js路径问题
2013/07/10 Javascript
基于javascript滚动图片具体实现
2013/11/18 Javascript
jQuery函数map()和each()介绍及异同点分析
2014/11/08 Javascript
Vue.js 单页面多路由区域操作的实例详解
2017/07/17 Javascript
基于AngularJs select绑定数字类型的问题
2018/10/08 Javascript
代码整洁之道(重构)
2018/10/25 Javascript
npm 常用命令详解(小结)
2019/01/17 Javascript
微信小程序HTTP接口请求封装的实现
2019/02/21 Javascript
Vue 图片压缩并上传至服务器功能
2020/01/15 Javascript
JavaScript函数重载操作实例浅析
2020/05/02 Javascript
python实现在windows服务中新建进程的方法
2015/06/30 Python
Python中的Django基本命令实例详解
2018/07/15 Python
python读出当前时间精度到秒的代码
2019/07/05 Python
Python爬虫 bilibili视频弹幕提取过程详解
2019/07/31 Python
一篇文章弄懂Python中的可迭代对象、迭代器和生成器
2019/08/12 Python
Python 正则表达式爬虫使用案例解析
2019/09/23 Python
Python3.5 win10环境下导入kera/tensorflow报错的解决方法
2019/12/19 Python
python读取csv文件指定行的2种方法详解
2020/02/13 Python
Python 实现日志同时输出到屏幕和文件
2020/02/19 Python
Python面向对象程序设计之私有变量,私有方法原理与用法分析
2020/03/23 Python
微软开源最强Python自动化神器Playwright(不用写一行代码)
2021/01/05 Python
Django和Ueditor自定义存储上传文件的文件名
2021/02/25 Python
快速一键生成Python爬虫请求头
2021/03/04 Python
英国No.1文具和办公用品在线:Euroffice
2016/09/21 全球购物
网络艺术零售业的先驱者:artrepublic
2017/09/26 全球购物
捷克体育用品购物网站:D-sport
2017/12/28 全球购物
企业演讲稿范文
2013/12/28 职场文书
初中学生评语大全
2014/04/24 职场文书
2014年办公室个人工作总结
2014/11/12 职场文书
python实现三阶魔方还原的示例代码
2021/04/28 Python
Python中Matplotlib的点、线形状、颜色以及绘制散点图
2022/04/07 Python