首页 > 解决方案 > 在 x 次请求后更改代理

问题描述

我正在尝试创建一个针对 API 检查用户名可用性的程序。但是,在大约 300 个请求后,我的速率受到限制。为了绕过这一点,我想在 100 个请求后使用轮换代理,但这并没有奏效,因为我收到了 429 个错误。有谁知道另一种方法或知道我做错了什么?

(旧)我试图绕过它:

    proxy = ''
    proxyFile = open(f'external/proxies.txt', 'r')
    proxyList = [line.split(',') for line in proxyFile.readlines()]
...

    def proxies(self):
        try: 
            if self.proxyCount > 0:
                self.proxyCount += 1
            proxy = self.proxyList[self.proxyCount]
            return proxy
        except:
            print(f"{Fore.LIGHTBLACK_EX}[+]{Fore.RESET} No more proxies")

    def checkAccounts(self):
        while not self.usernames.empty():
            name = self.usernames.get(); self.usernames.put(name)
            url = f"https://public-ubiservices.ubi.com/v3/profiles?nameOnPlatform={name}&platformType=uplay"
            try:         
                r = self.session.get(url, headers=self.headers)
                
                try:
                    if self.checkedCount % 100 == 0:
                        self.proxies()
                except:
                    pass

                ctypes.windll.kernel32.SetConsoleTitleW(f"Gx | Checked: {self.checkedCount}, Available: {self.availableCount}, Errors: {self.errorCount}")
                if r.status_code == 200:

                    if len(r.json()['profiles']) != 0:
                        print(f"{Fore.LIGHTBLACK_EX}[+]{Fore.RESET} Taken:          {name}")
                        self.checkedCount += 1
                        
                    else:  
                        print(f"{Fore.LIGHTBLACK_EX}[+]{Fore.RESET} Available:      {name}")
                        self.availableCount += 1
                        self.checkedCount += 1

                else:
                    print(f"{Fore.LIGHTBLACK_EX}[+]{Fore.RESET} Error:          Check errors.txt")
                    with open('external/errors.txt', "a") as errorFile:
                        errorFile.write(f'{self.currentTime} | Checking error {r.status_code}, Error message: {r.text}\n')

Edit1(代理功能现在可以工作,但仍然受到速率限制):

url = f"https://public-ubiservices.ubi.com/v3/profiles?nameOnPlatform={name}&platformType=uplay"
try:         
    r = requests.get(url, headers=self.headers, proxies=self.proxy)
    try:
        if self.checkedCount % 100 == 0:
        self.proxies()
    except:
        pass
...

if r.status_code == 429:
    self.errorCount += 1
    print(f"{Fore.LIGHTBLACK_EX}[+]{Fore.RESET} Error:          Rate limited")
    self.proxies()

...
    def proxies(self):
        try: 
            if self.proxyCount > 0:
                self.proxyCount += 1
            proxy = random.choice(self.proxyList)
            print(proxy)
            return proxy
        except:
            pass

编辑1的输出

['14.140.131.82:3128\n']
[+] Error:          Rate limited
['89.133.95.177:18080\n']
['36.91.45.10:51672\n']
[+] Error:          Rate limited
['36.91.45.10:51672\n']
['85.214.65.246:80\n']
[+] Error:          Rate limited

标签: pythonpython-requestsproxies

解决方案


推荐阅读