Sep-01-2021, 06:23 AM
Hi guys,
I've been learning about rotating proxies and have found myself a little stuck and after many many hours have passed, I thought it was time to reach out for some assistance.
In a nutshell, I've got a list of proxies and I want pick a random one from a list for each request. If the random list finds a proxy that works, the code below works correctly. If it tries to use a proxy that doesn't work, I get:
So I've got:
But anyway, my goal of the above code is to grab a random proxy from the list, test it to check if it works and if it does, do the request. Alteratively if it doesn't, keep randomly looping through the proxy list until it can find a working proxy- and then go ahead and complete the request.
Can anyone please enlighten me how this can be done?
Thanks a lot.
I've been learning about rotating proxies and have found myself a little stuck and after many many hours have passed, I thought it was time to reach out for some assistance.

In a nutshell, I've got a list of proxies and I want pick a random one from a list for each request. If the random list finds a proxy that works, the code below works correctly. If it tries to use a proxy that doesn't work, I get:
Error:ConnectTimeout: HTTPSConnectionPool(host='hostname.com', port=443): Max retries exceeded with url: /google.com
I understand that the error is the proxy not working(I tested it with several working proxies to verify the problem), so what I'm trying to do, is to run a loop to find a random proxy from my list each time a request is made. So I've got:
from bs4 import BeautifulSoup import requests import random url = ‘testurl.com’ proxy_list = ['173.68.59.131:3128','64.124.38.139:8080','69.197.181.202:3128'] proxies = random.choice(proxy_list) response = requests.get(url, headers=headers, proxies={'https': proxies}, timeout=3) if response.status_code == 200: print(response.status_code) elif response.status_code != 200: proxies = random.choice(proxy_list) response = requests.get(url, headers=headers, proxies={'https': proxies}, timeout=3)(At the moment, the code is simply printing a response code of 200 if it's successful, but I'll be changing that later to get html information.)
But anyway, my goal of the above code is to grab a random proxy from the list, test it to check if it works and if it does, do the request. Alteratively if it doesn't, keep randomly looping through the proxy list until it can find a working proxy- and then go ahead and complete the request.
Can anyone please enlighten me how this can be done?
Thanks a lot.