简体   繁体   English

我如何每次选择一个随机代理

[英]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.我有一个脚本,它连接到我的 proxies.txt 文件中的一个随机代理,我已经验证它连接成功,所以它可以工作。 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:代理.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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为每次出现的每个球选择随机颜色? - How to choose a random color for each ball that will show up every time? 每次运行 for 循环时如何打印随机密码 - How do I print random passwords every time I run through a for loop 每次单击按钮时如何更改 random_number 变量? - How do I change the random_number variable every time I click the button? 每次运行 function 时,如何生成具有随机属性的不同新对象? - How do i generate different new objects with random attributes every time i run a function? 如何在Python 2.7中选择3个随机数? - How do I choose between 3 random numbers in Python 2.7? 除了在索引中使用random.randint()之外,如何从python列表中选择随机字符串? - How do i choose random string from python list other than using random.randint() in index? 如何让 Python 选择符合要求的最大随机值? - How do I make Python choose the largest random value which fits the requirements? 如何从 2 个列表中选择 2 个随机元素,然后将它们弹出,这样它们就不会被再次使用 - How do I choose 2 random elements from 2 lists and then.pop them so they won't be used again 如何允许Python从2个文本文件(同一行)中选择一条随机行,然后将其存储为变量? - How do I allow Python to choose a random line from 2 text files (that are the same line) and then store them as variables? 如何选择随机海龟来移动随机数量的步数? - How can I choose a random turtle to move a random amount of steps?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM