python 获取微信好友列表的方法(微信web)

yipeiwu_com5年前Python基础

如下所示:

 
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 使用指定的网卡发送HTTP请求的实例

需求: 一台机器上有多个网卡, 如何访问指定的 URL 时使用指定的网卡发送数据呢? $ curl --interface eth0 www.baidu.com # curl...

Python2和Python3之间的str处理方式导致乱码的讲解

Python字符串问题 在arcpy中版本为 python2.x 在QGIS中版本为 python2.x 或者 python3.x python2 和python3 之间的...

利用python实现xml与数据库读取转换的方法

前言 xml课的第三第四个作业都是用java编程来实现xml dom的一些转换, 因为自己没怎么学过java,因此和老师说了下想用python来实现第三第四个作业,下面就直接贴代码了 x...

python 并发编程 阻塞IO模型原理解析

python 并发编程 阻塞IO模型原理解析

阻塞IO(blocking IO) 在linux中,默认情况下所有的socket都是blocking,一个典型的读操作流程大概是这样: 当用户进程调用了recvfrom这个系统调用,k...

利用python对Excel中的特定数据提取并写入新表的方法

最近刚开始学python,正好实习工作中遇到对excel中的数据进行处理的问题,就想到利用python来解决,也恰好练手。 实际的问题是要从excel表中提取日期、邮件地址和时间,然后统...