1.先獲取uuid
這個其實類似獲取驗證碼一樣,每次刷新都會不一樣秦忿,你只需要拿一次刷新的結(jié)果就好
//獲取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
//返回數(shù)據(jù)
window.QRLogin.code = 200;
window.QRLogin.uuid = "";
2.通過獲取的uuid,獲取授權(quán)二維碼
//返回的是一張二維碼圖耍铜,手機掃描即可
https://login.weixin.qq.com/qrcode/(uuid的值)
3.獲取登陸地址
//構(gòu)造以下地址,掃描步驟1的二維碼,手機確定后打開構(gòu)造好的地址
https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=(uui的值)
//返回數(shù)據(jù)
window.code = 200;
window.redirect_uri ="登陸成功的跳轉(zhuǎn)地址";
4.獲得跳轉(zhuǎn)鏈接后搬卒,就可以登陸了
該鏈接是獨立的,隨便用一個瀏覽器打開都可以
5.代碼實現(xiàn)登陸翎卓,并獲取所有聯(lián)系人契邀,公眾號及群聊信息
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()
# 獲取授權(quán)二維碼
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('登陸失敗!')
演示:
show.gif
6.其實itchat包就是利用模擬登陸來實現(xiàn)的
有興趣的可以去看他的官網(wǎng),或者源碼:itchat文檔
寫在最后失暴,當某一天你也設(shè)計網(wǎng)站的綁定登陸不妨參考一下微信的方式