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多线程编程(八):使用Event实现线程间通信
Apr 05 Python
python输出当前目录下index.html文件路径的方法
Apr 28 Python
Python正则抓取网易新闻的方法示例
Apr 21 Python
Anaconda多环境多版本python配置操作方法
Sep 12 Python
python selenium UI自动化解决验证码的4种方法
Jan 05 Python
django加载本地html的方法
May 27 Python
pycharm访问mysql数据库的方法步骤
Jun 18 Python
python3 下载网络图片代码实例
Aug 27 Python
在Django下测试与调试REST API的方法详解
Aug 29 Python
利用Python小工具实现3秒钟将视频转换为音频
Oct 29 Python
tensorflow入门:TFRecordDataset变长数据的batch读取详解
Jan 20 Python
Python django框架开发发布会签到系统(web开发)
Feb 12 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
模仿OSO的论坛(三)
2006/10/09 PHP
PHP操作xml代码
2010/06/17 PHP
ecshop实现smtp发送邮件
2015/02/03 PHP
微信公众平台开发实现2048游戏的方法
2015/04/15 PHP
Zend Framework动作助手Json用法实例分析
2016/03/05 PHP
LaravelS通过Swoole加速Laravel/Lumen详解
2018/03/02 PHP
thinkphp5使html5实现动态跳转的例子
2019/10/16 PHP
Maps Javascript
2007/01/22 Javascript
学习ExtJS(一) 之基础前提
2009/10/07 Javascript
使用JavaScript库还是自己写代码?
2010/01/28 Javascript
javascript创建和存储cookie示例
2014/01/07 Javascript
禁止拷贝网页内容的js代码
2014/01/22 Javascript
jquery中get和post的简单实例
2014/02/04 Javascript
javascript读写json示例
2014/04/11 Javascript
使用javascript获取页面名称
2014/12/23 Javascript
js限制文本框只能输入中文的方法
2015/08/11 Javascript
JavaScript数据结构与算法之链表
2016/01/29 Javascript
Vue2学习笔记之请求数据交互vue-resource
2017/02/23 Javascript
vue和webpack项目构建过程常用的npm命令详解
2018/06/15 Javascript
非常实用的jQuery代码段集锦【检测浏览器、滚动、复制、淡入淡出等】
2019/08/08 jQuery
基于ajax及jQuery实现局部刷新过程解析
2020/09/12 jQuery
javascript实现打砖块小游戏(附完整源码)
2020/09/18 Javascript
[43:33]EG vs Spirit Supermajor 败者组 BO3 第一场 6.4
2018/06/05 DOTA
python访问系统环境变量的方法
2015/04/29 Python
Win7下搭建python开发环境图文教程(安装Python、pip、解释器)
2016/05/17 Python
Python 爬虫学习笔记之多线程爬虫
2016/09/21 Python
python中lambda()的用法
2017/11/16 Python
解决pycharm同一目录下无法import其他文件
2020/02/12 Python
html5 css3 动态气泡按钮实例演示
2012/12/02 HTML / CSS
会计实习生工作总结的自我评价
2013/10/07 职场文书
放飞中国梦演讲稿
2014/04/23 职场文书
房地产公司见习自我鉴定
2014/04/28 职场文书
CSS完成视差滚动效果
2021/04/27 HTML / CSS
无线电通信名词解释
2022/02/18 无线电
Netty分布式客户端处理接入事件handle源码解析
2022/03/25 Java/Android
科学家测试在太空中培育人造肉,用于未来太空旅行
2022/04/29 数码科技