gpt4 book ai didi

python - Selenium Webdriver Firefox 52 Python 每次运行时选择随机代理

转载 作者:太空宇宙 更新时间:2023-11-03 15:17:13 25 4
gpt4 key购买 nike

我尝试为 FireFox 上的每次运行设置一个新的随机代理。我尝试了很多方法,但只有这个有效,但无法弄清楚如何使其随机:

profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", "Host")
profile.set_preference("network.proxy.http_port", port)
browser = webdriver.Firefox(profile)

我尝试了这个例子,但没有成功:

from selenium.webdriver.common.proxy import *
myProxy = "xx.xx.xx.xx:xxxx"

proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': myProxy,
'ftpProxy': myProxy,
'sslProxy': myProxy,
'noProxy': '' # set this value as desired
})
driver = webdriver.Firefox(proxy=proxy)
driver.get("http://www.google.com")

这对我来说是最好的方法,因为我可以使用:

myProxy = random.choice(open('data.txt').readlines())

我尝试从文本文件中获取代理,但不知道如何随机化:

with open('IPs.txt') as proxylist:
for line in proxylist:
proxyserv, proxyport = line.split(':')
proxy= proxyserv , proxyport

最后我尝试了:

def random_line():
line_num = 0
selected_line = ''
with open('IPs.txt') as f:
while 1:
line = f.readline()
if not line: break
line_num += 1
if random.uniform(0, line_num) < 1:
selected_line = line
return selected_line.strip()

这个得到随机行,但无法弄清楚如何将结果解析为X=IPY=端口然后:

profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", "RANDOM IP")
profile.set_preference("network.proxy.http_port", Random PORT)
browser = webdriver.Firefox(profile)

最佳答案

Port 需要是一个整数,您可能需要使用:

import random
myProxy = random.choice(open('IPs.txt').readlines())
parts = myProxy.strip().split(":") # strip removes spaces and line breaks
host = parts[0]
port = int(parts[1]) # port needs to be an integer

关于python - Selenium Webdriver Firefox 52 Python 每次运行时选择随机代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43780696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com