下面是“Python3.2模拟实现webqq登录”的完整攻略,主要分为以下几步:
准备工作
-
安装Python 3.2及以上版本,并配置好环境变量。
-
安装requests模块,这个模块是用来发送HTTP请求的,可以通过pip安装:
pip install requests
-
获取WebQQ登录所需的一些参数,主要有以下几个:
-
ptwebqq:通过访问https://s.web2.qq.com,查看Cookie中名为ptwebqq的值。该参数是登录WebQQ时所需的主要参数。
-
g_login_sig:通过访问https://d1.web2.qq.com,查看Cookie中名为g_login_sig的值。该参数是登录WebQQ时所需的主要参数。
-
ptqrtoken:通过访问https://ptlogin2.qq.com/check,查看返回的含有ptqrtoken参数的url。该参数是处理二维码时所需的主要参数。
-
session:通过访问https://s.web2.qq.com,查看Cookie中名为skey的值。该参数是登录WebQQ后所需的主要参数。
-
vfwebqq:通过访问https://s.web2.qq.com,查看返回的JSON数据中含有vfwebqq参数的值。该参数是处理消息时所需的主要参数。
实现代码
以下是Python代码实现WebQQ登录过程的完整攻略:
import time
import random
import hashlib
import requests
def get_login_url(qr_code_url):
"""
处理二维码,获取登录后的URL
"""
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
}
response = requests.get(qr_code_url, headers=headers)
ptqrtoken = response.cookies.get('qrsig')
print('请扫描二维码登录...')
while True:
time.sleep(1)
check_url = 'https://ssl.ptlogin2.qq.com/check?ptqrtoken={}&webqq_type=10&remember_uin=1&login2qq=1&aid=501004106&u1=https%3A%2F%2Fqq.com%2F&ptredirect=0&h=1&from_ui=1&pttype=1&dumy=&fp=loginerroralert&action=0-0-{}'.format(ptqrtoken, int(time.time()*1000))
check_response = requests.get(check_url, headers=headers)
if '登录成功' in check_response.text:
return check_response.text.split('\'')[1]
def get_login_cookies(qr_code_url):
"""
获取登录后的Cookie
"""
login_url = get_login_url(qr_code_url)
ptcz = ''
uin = ''
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Referer': 'https://xui.ptlogin2.qq.com/cgi-bin/xlogin?appid=501004106&daid=1&pt_no_auth=1&s_url=https://web2.qq.com/proxy.html&style=40&low_login=0&proxy_url=https://web2.qq.com/proxy.html&hide_title_bar=1&hide_close_icon=1&no_verifyimg=1&no_qrcode=0&self_regurl=https://ui.ptlogin2.qq.com/cgi-bin/login?proxy_url=https://web2.qq.com/proxy.html&pt_i_from=&ptlang=2052&ptredirect=0&aid=501004106&login_sig=&pt_uistyle=40&pt_jstoken=401524137',
}
response = requests.get(login_url, headers=headers)
for cookie in response.cookies:
if cookie.name == 'ptcz':
ptcz = cookie.value
elif cookie.name == 'uin':
uin = cookie.value
return {
'ptwebqq': response.cookies.get('ptwebqq'),
'pgv_pvid': '5878843884',
'pgv_info': 'ssid=s6840495612',
'skey': response.cookies.get('skey'),
'p_uin': uin,
'p_skey': response.cookies.get('p_skey'),
'ptcz': ptcz,
'qrsig': '', # 这个字段可以不用设置,因为已经获取到了。
'uin': uin,
}
def get_vfwebqq(cookies):
"""
获取vfwebqq参数
"""
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Referer': 'https://s.web2.qq.com/proxy.html?v=20130916001&callback=1&id=1',
}
url = 'https://s.web2.qq.com/api/getvfwebqq?ptwebqq={}&clientid=53999199&psessionid=&t={}'.format(cookies['ptwebqq'], int(time.time()))
response = requests.get(url, headers=headers, cookies=cookies)
return response.json()['result']['vfwebqq']
def get_psessionid_and_uid(cookies):
"""
获取psessionid和uid参数
"""
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Referer': 'https://d1.web2.qq.com/cfproxy.html?v=20151105001&callback=1',
}
url = 'https://d1.web2.qq.com/channel/login2'
r = str(int(random.random()*100000000))
login_data = {
'r': r,
'pt2gguin': cookies['uin'],
'ptwebqq': cookies['ptwebqq'],
'clientid': 53999199,
'psessionid': '',
'status': 'online',
}
psessionid_header = {
'Referer': 'https://d1.web2.qq.com/proxy.html?v=20151105001&callback=1&id=2',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Origin': 'https://d1.web2.qq.com',
}
response = requests.post(url, headers=headers, cookies=cookies, json=login_data)
psessionid = response.json()['result']['psessionid']
uid = response.json()['result']['uin']
return psessionid, uid
def get_group_list(psessionid, cookies):
"""
获取群组列表
"""
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Referer': 'https://d1.web2.qq.com/cfproxy.html?v=20151105001&callback=1',
}
url = 'https://s.web2.qq.com/api/get_group_name_list_mask2'
r = str(int(random.random()*100000000))
data = {
'r': r,
'vfwebqq': get_vfwebqq(cookies),
'hash': hashlib.md5((str(cookies['p_uin'])+psessionid).encode('utf-8')).hexdigest(),
}
response = requests.post(url, headers=headers, cookies=cookies, json=data)
return response.json()
if __name__ == '__main__':
qr_code_url = 'https://ptlogin2.qq.com/ptqrshow?appid=501004106&e=2&l=M&s=3&d=72&v=4&t=0.5257469508827072'
cookies = get_login_cookies(qr_code_url)
psessionid, uid = get_psessionid_and_uid(cookies)
group_list = get_group_list(psessionid, cookies)
print(group_list)
示例说明
为了更好的说明上述程序,这里给出两个示例:
示例一
输入以下代码即可运行第一个示例:
if __name__ == '__main__':
qr_code_url = 'https://ptlogin2.qq.com/ptqrshow?appid=501004106&e=2&l=M&s=3&d=72&v=4&t=0.5257469508827072'
cookies = get_login_cookies(qr_code_url)
psessionid, uid = get_psessionid_and_uid(cookies)
group_list = get_group_list(psessionid, cookies)
print(group_list)
运行该程序后,会输出WebQQ所有的群组名称及其gid和code值。示例输出如下:
{'retcode': 0, 'result': {'gnamelist': [{'flag': 16778241, 'name': '某某联盟(006)', 'gid': 1929842108, 'code': 530648501, 'owner': 835698566, 'createtime': 1479142131, 'level': 5}, {'flag': 81, 'name': '客户群', 'gid': 875150207, 'code': 3084357681, 'owne...
示例二
下面是另一个示例,该示例演示如何发送WebQQ消息。输入以下代码即可运行:
import json
def send_message(qq_number, message_content, psessionid, cookies):
"""
发送WebQQ消息
"""
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Referer': 'https://d1.web2.qq.com/cfproxy.html?v=20151105001&callback=1&id=2',
}
url = 'https://d1.web2.qq.com/channel/send_buddy_msg2'
r = str(int(random.random()*100000000))
data = {
'r': r,
'to': qq_number,
'face': 0,
'content': json.dumps([message_content, ['font', {'name': '宋体', 'size': '10', 'style': [0, 0, 0], 'color': '000000'}]]),
'msg_id': int(time.time()*1000),
'clientid': 53999199,
'psessionid': psessionid,
}
response = requests.post(url, headers=headers, cookies=cookies, json=data)
return response.json()
if __name__ == '__main__':
qq_number = 12345 # 这里填入测试对象的QQ号码。
message_content = 'Hello, World!' # 这里填入消息内容。
qr_code_url = 'https://ptlogin2.qq.com/ptqrshow?appid=501004106&e=2&l=M&s=3&d=72&v=4&t=0.5257469508827072'
cookies = get_login_cookies(qr_code_url)
psessionid, uid = get_psessionid_and_uid(cookies)
send_message(qq_number, message_content, psessionid, cookies)
运行该程序后,会自动发送消息给指定的WebQQ用户。注意,此处需要手动输入测试用户的QQ号码,以及要发送的消息内容。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python3.2模拟实现webqq登录 - Python技术站