简体   繁体   中英

How do I choose a random proxy every time

I have a script where it connects to a random proxy in my proxies.txt file, I have verified that it connects successfully so it does work. But, when the code is running every time I call the function it will connect to the same proxy it chose in the beginning. I want it to change proxy every time I call it.

def get_single_proxy():
    proxy_list = [line.replace('\n', '') for line in open('proxies.txt', 'r')]
    proxy = random.choice(proxy_list)
    return proxy

PROXY = get_single_proxy()

chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=3840x2160")
chrome_options.add_argument('--proxy-server=%s' % PROXY)

driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)

async def start(ctx):
    driver.get(URL)
    print(PROXY)

UPDATE: Following advice from below,

class ProxyRotator:
    def __init__(self):
        #self.proxylist = [line.replace('\n', '') for line in open('proxies.txt', 'r')]
        self.proxyList = ['45.72.40.18:80', '45.130.127.12:80', '45.87.243.138:80']

    def get(self):
        """
        Optionally you could shuffle self.proxyList every X minutes or 
        after all proxies had been fetched once ...
        """
        proxy = self.proxyList.pop(0)
        self.proxyList.append(proxy)
        return proxy


pr = ProxyRotator()
for x in range(6):
    print(pr.get())
    
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=3840x2160")
chrome_options.add_argument('--proxy-server=%s' % pr)

driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)

async def start(ctx):
    driver.get(URL)
    print(pr)

A possible solution is using class to store actual proxy and then always generate a different from the actual as show code bellow:

import random

class Proxy():
    def __init__(self):
        self.actual_proxy = None

    def get_single_proxy(self):
        proxy_list = [line.replace('\n', '') for line in open('proxies.txt', 'r')]
        if not proxy_list:
            raise ValueError("proxy_list is empty")
        while True:
            proxy = random.choice(proxy_list)
            if proxy != self.actual_proxy:
                self.actual_proxy = proxy
                break
        return proxy

p = Proxy()


for i in range(20):
    PROXY = p.get_single_proxy()
    print(f'rotate {i:02d} -> {PROXY}')

OUTPUT:

rotate 00 -> 50.50.50.50
rotate 01 -> 30.30.30.30
rotate 02 -> 90.90.90.90
rotate 03 -> 80.80.80.80
rotate 04 -> 70.70.70.70
rotate 05 -> 80.80.80.80
rotate 06 -> 40.40.40.40
rotate 07 -> 70.70.70.70
rotate 08 -> 30.30.30.30
rotate 09 -> 50.50.50.50
rotate 10 -> 80.80.80.80
rotate 11 -> 40.40.40.40
rotate 12 -> 10.10.10.10
rotate 13 -> 70.70.70.70
rotate 14 -> 40.40.40.40
rotate 15 -> 50.50.50.50
rotate 16 -> 80.80.80.80
rotate 17 -> 90.90.90.90
rotate 18 -> 20.20.20.20
rotate 19 -> 10.10.10.10

proxies.txt:

10.10.10.10
20.20.20.20
30.30.30.30
40.40.40.40
50.50.50.50
60.60.60.60
70.70.70.70
80.80.80.80
90.90.90.90

You could pop the first proxy from the list and append it to the end:

class ProxyRotator:
    def __init__(self):
        # self.proxy_list = [line.replace('\n', '') for line in open('proxies.txt', 'r')]
        self.proxyList = ['proxy1', 'proxy2', 'proxy3']

    def get(self):
        """
        Optionally you could shuffle self.proxyList every X minutes or 
        after all proxies had been fetched once ...
        """
        proxy = self.proxyList.pop(0)
        self.proxyList.append(proxy)
        return proxy


pr = ProxyRotator()
for x in range(6):
    print(pr.get())

Out:

proxy1
proxy2
proxy3
proxy1
proxy2
proxy3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM