1.先获取uuid
这个其实类似获取验证码一样,每次刷新都会不一样,你只需要拿一次刷新的结果就好
//获取uuid的地址
https://login.wx.qq.com/jslogin?appid=wx782c26e4c19acffb&redirect_uri=https%3A%2F%2Flogin.weixin.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage&fun=new&lang=zh_CN
//返回数据
window.QRLogin.code = 200;
window.QRLogin.uuid = "";
2.通过获取的uuid,获取授权二维码
//返回的是一张二维码图,手机扫描即可
https://login.weixin.qq.com/qrcode/(uuid的值)
3.获取登陆地址
//构造以下地址,扫描步骤1的二维码,手机确定后打开构造好的地址
https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=(uui的值)
//返回数据
window.code = 200;
window.redirect_uri ="登陆成功的跳转地址";
4.获得跳转链接后,就可以登陆了
该链接是独立的,随便用一个浏览器打开都可以
5.代码实现登陆,并获取所有联系人,公众号及群聊信息
import requests
import os
import time
import json
wx_web = 'https://wx.qq.com/cgi-bin/mmwebwx-bin'
wx_login = 'https://login.wx.qq.com'
session = requests.Session()
def step1_get_uuid():
url = wx_login + '/jslogin?appid=wx782c26e4c19acffb&redirect_uri=https%3A%2F%2Flogin.weixin.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage'
text = session.get(url).text
uuid = text[text.rfind('uuid = "') + len('uuid = "'):len(text) - 2]
return uuid
def step2_get_qr_code(uuid, timeout=6):
url = wx_login + '/qrcode/%s' % uuid
content = session.get(url).content
with open('qr_code.png', 'wb') as fp:
fp.write(content)
os.system('qr_code.png')
time.sleep(timeout)
os.remove('qr_code.png')
def step3_login_address(uuid):
url = wx_login + '/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=%s' % uuid
text = session.get(url).text
if text.find('window.code=200;') >= 0:
return text[text.find('redirect_uri="') + len('redirect_uri="'):len(text) - 2]
else:
return None
def step4_open_it(url):
session.get(url)
def step5_get_all_info():
contact_url = '%s/webwxgetcontact' % wx_web
data = {'BaseRequest': ''}
r = session.post(contact_url, data=json.dumps(data))
json_data = json.loads(r.content.decode('utf-8'))
return json_data
if __name__ == '__main__':
# 获取uuid
uuid = step1_get_uuid()
# 获取授权二维码
step2_get_qr_code(uuid)
# 获取登陆地址
url = step3_login_address(uuid)
if url is not None:
# 打开登陆地址
step4_open_it(url)
# 获取所有信息
data = step5_get_all_info()
mp_type = {
8: {'name': '个人订阅号', 'count': 0, 'list': []},
0: {'name': '好友', 'count': 0, 'list': [],
'gender': {'man': 0, 'woman': 0, 'undefined': 0}},
24: {'name': '商标公众号', 'count': 0, 'list': []},
56: {'name': '微信团队', 'count': 0, 'list': []},
29: {'name': '未知', 'count': 0, 'list': []},
-1: {'name': '群聊', 'count': 0, 'list': []}
}
for x in data['MemberList']:
if x['UserName'].count('@') == 2:
mp_type[-1]['count'] += 1
mp_type[-1]['list'].append(x['NickName'])
else:
mp_type[x['VerifyFlag']]['count'] += 1
if x['VerifyFlag'] is not 0:
mp_type[x['VerifyFlag']]['list'].append(x['NickName'])
else:
if x['Sex'] == 1:
mp_type[x['VerifyFlag']]['gender']['man'] += 1
elif x['Sex'] == 2:
mp_type[x['VerifyFlag']]['gender']['woman'] += 1
else:
mp_type[x['VerifyFlag']]['gender']['undefined'] += 1
for key, item in mp_type.items():
print(item)
print(100 * '-')
else:
print('登陆失败!')
演示:
6.其实itchat包就是利用模拟登陆来实现的
有兴趣的可以去看他的官网,或者源码:itchat文档
写在最后,当某一天你也设计网站的绑定登陆不妨参考一下微信的方式