Backend Development 13 min read

Implementing Local QR Code Login for QQ Music Using Python

This tutorial walks through building a Python script that fetches a QQ Music QR code, displays it locally, monitors its status, extracts required encrypted parameters from cookies, and completes the login process by repeatedly polling the authentication endpoint to obtain a session.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Implementing Local QR Code Login for QQ Music Using Python

The article demonstrates how to create a local QR‑code login flow for QQ Music using Python, covering the entire process from retrieving the QR image to completing authentication and obtaining a usable session.

Preparation : Define helper functions to show, remove, and save images across different operating systems, handling both system calls and fallback to Pillow when necessary.

import sys import os import subprocess '''用于在不同OS显示验证码''' def showImage(img_path): try: if sys.platform.find('darwin') >= 0: subprocess.call(['open', img_path]) elif sys.platform.find('linux') >= 0: subprocess.call(['xdg-open', img_path]) else: os.startfile(img_path) except: from PIL import Image img = Image.open(img_path) img.show() img.close() '''验证码验证完毕后关闭验证码并移除''' def removeImage(img_path): if sys.platform.find('darwin') >= 0: os.system("osascript -e 'quit app \"Preview\"'") os.remove(img_path) '''保存验证码图像''' def saveImage(img, img_path): if os.path.isfile(img_path): os.remove(img_path) fp = open(img_path, 'wb') fp.write(img) fp.close()

Fetching the QR code : Use the QQ Music QR login endpoint https://ssl.ptlogin2.qq.com/ptqrshow with required parameters (appid, e, l, s, d, v, t, daid, pt_3rd_aid). The t parameter is a random float generated by random.random() . The response content is saved as qrcode.jpg and displayed locally.

session = requests.Session() params = { 'appid': '716027609', 'e': '2', 'l': 'M', 's': '3', 'd': '72', 'v': '4', 't': str(random.random()), 'daid': '383', 'pt_3rd_aid': '100497308', } response = session.get('https://ssl.ptlogin2.qq.com/ptqrshow?', params=params) saveImage(response.content, os.path.join(os.getcwd(), 'qrcode.jpg')) showImage(os.path.join(os.getcwd(), 'qrcode.jpg')) qrsig = session.cookies.get('qrsig')

Extracting encrypted parameters : The login process requires pt_login_sig (directly from cookies) and ptqrtoken , which is derived from the qrsig cookie using the hash33 algorithm. The hash33 implementation in Python mirrors the JavaScript version.

def __decryptQrsig(qrsig): e = 0 for c in qrsig: e += (e << 5) + ord(c) return 2147483647 & e pt_login_sig = session.cookies.get('pt_login_sig') ptqrtoken = __decryptQrsig(qrsig)

Polling the login status : Repeatedly call https://ssl.ptlogin2.qq.com/ptqrlogin with parameters including u1 , ptqrtoken , action (timestamp‑based), and the previously obtained pt_login_sig . The loop continues until the response indicates a successful login, after which the QR image is removed.

while True: params = { 'u1': 'https://graph.qq.com/oauth2.0/login_jump', 'ptqrtoken': ptqrtoken, 'ptredirect': '0', 'h': '1', 't': '1', 'g': '1', 'from_ui': '1', 'ptlang': '2052', 'action': '0-0-%s' % int(time.time() * 1000), 'js_ver': '20102616', 'js_type': '1', 'login_sig': pt_login_sig, 'pt_uistyle': '40', 'aid': '716027609', 'daid': '383', 'pt_3rd_aid': '100497308', 'has_onekey': '1', } resp = session.get('https://ssl.ptlogin2.qq.com/ptqrlogin?', params=params) if '二维码未失效' in resp.text or '二维码认证中' in resp.text: pass elif '二维码已经失效' in resp.text: raise RuntimeError('Fail to login, qrcode has expired') else: break time.sleep(0.5) removeImage(os.path.join(os.getcwd(), 'qrcode.jpg'))

Finalizing login : Extract the QQ number and a redirect URL from the successful response, follow the redirect to complete authentication, and return the authenticated requests.Session object.

qq_number = re.findall(r'&amp;uin=(.+?)&amp;service', resp.text)[0] url_refresh = re.findall(r"'(https:.*?)'", resp.text)[0] session.get(url_refresh, allow_redirects=False, verify=False) print('Account "%s" logged in successfully' % qq_number) return session

The complete script combines all the above steps into a class qqmusicScanqr with an entry point that creates an instance and calls login() to obtain a ready‑to‑use session for further QQ Music API interactions.

PythonAutomationqq-musicweb scrapingRequestsQR Login
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.