python - 在 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
解决方案
推荐阅读
- javascript - API 数据不会在生产中更新,但在本地工作
- javascript - 复制到剪贴板的按钮仅适用于几个 div 中的第一个
- crystal-reports - Crystal Report 中的数据库连接器错误
- spring-boot - 为什么集成测试的 Jacoco 覆盖率为 0%
- powershell - 如何遍历 PSSession 中的多台机器,以便我对正确的机器执行 Invoke-Command?
- javascript - React Native,Redux 找不到上下文
- java - 如何生成活动通知
- coinbase-api - 是否可以从 coinbase API 获取过去特定时间的美元比特币价格?
- python - How can I open the Edge browser with Selenium to maintain login status
- aws-lambda - Running Codebuild for different Sources