当我将代理变量放在for循环内时,代码可以正常工作,但是当我在for循环外打印代理时,它仅给出第一个循环并停止,如果我在for循环外使用代理变量,则有一种方法可以正常工作。
value = ['3128', '3128', '3128', '3128']
num = ['40.69.144.143', '40.69.144.143', '40.69.144.143', '40.69.144.143']
for (a, c) in zip(num,value):
proxy = a+':'+c
print(proxy)
为了解释正在做的事情,正在做一个鞋机器人,它同时打开多个窗口,并且每个窗口都有自己的唯一代理,我想将proxy变量作为参数放入函数get_chromedriver中。
from selenium import webdriver
import threading
import time
import zipfile
proxy_host = ['40.122.24.111', '40.122.24.111', '40.122.24.111', '40.122.24.111'] # rotating proxy
proxy_port = ['3128', '3128', '3128', '3128']
proxy_username = ['lukmanaraby', 'lukmanaraby', 'lukmanaraby', 'lukmanaraby']
proxy_password = ['aiobotagents', 'aiobotagents', 'aiobotagents', 'aiobotagents']
for (a, c) in zip(proxy_host,proxy_port):
proxy = a+':'+c
def test_logic():
url = 'https://whatismyipaddress.com/'
webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe", options=chrome_options).get(url)
# Implement your test logic
def auth_json(host,port,user,password):
global manifest_json
global background_js
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
"""
background_js = """
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "%s",
port: parseInt(%s)
},
bypassList: ["localhost"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "%s",
password: "%s"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
""" % (host,port,user,password)
#containing the authentication of the proxies.
def proxy_auth(): #transfering the information of the proxies to auth_json function
for (i,j,m,k) in zip(proxy_host, proxy_port, proxy_username, proxy_password):
auth_json(i,j,m,k)
def get_chromedriver(use_proxy=False, user_agent=proxy):
global chrome_options
chrome_options = webdriver.ChromeOptions()
if use_proxy:
pluginfile = 'proxy_auth_plugin.zip'
with zipfile.ZipFile(pluginfile, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
chrome_options.add_extension(pluginfile)
if user_agent==proxy:
chrome_options.add_argument('--proxy-server=%s' % user_agent)
driver = webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe",options=chrome_options)
return driver
N = 5 # Number of browsers to spawn
thread_list = list()
# Start test
for i in proxy_host:
get_chromedriver()
proxy_auth()
t = threading.Thread(name='Test {}'.format(i), target=test_logic)
t.start()
time.sleep(1)
print (t.name + ' started!')
thread_list.append(t)
# Wait for all thre<ads to complete
for thread in thread_list:
thread.join()
print ('Test completed!')
enter code here
我真的想不出办法在保持变量类型与现在相同的情况下使其工作(str)。如果在for
循环内分配了一个值并在循环外访问,它将仅保留在循环终止之前分配的最后一个值。
最好的方法是创建一个列表变量并将值附加到循环内的列表中。之后,可以从结果列表轻松访问所有值:
value = ['3128', '3128', '3128', '3128']
num = ['40.69.144.143', '40.69.144.143', '40.69.144.143', '40.69.144.143']
proxy = []
for (a, c) in zip(num,value):
proxy.append(a+':'+c)
for item in proxy:
print(item)
按照下面的注释进行更新:要在函数中使用结果变量而不将值分配给列表,您可以将函数调用放在for
循环内。
def my_func(a):
return a
value = ['3128', '3128', '3128', '3128']
num = ['40.69.144.143', '40.69.144.143', '40.69.144.143', '40.69.144.143']
for (a, c) in zip(num,value):
proxy.append(a+':'+c)
foo = my_func(proxy)
编辑2:所以我看了一下您的函数,似乎很容易修改它,以使用列表而不是简单的字符串进行修改。这样,函数将基于多个代理返回多个驱动程序的列表:
def get_chromedriver(use_proxy=False, user_agent: list = proxy):
global chrome_options
chrome_options = webdriver.ChromeOptions()
if use_proxy:
pluginfile = 'proxy_auth_plugin.zip'
with zipfile.ZipFile(pluginfile, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
chrome_options.add_extension(pluginfile)
drivers = []
for i in user_agent:
chrome_options.add_argument('--proxy-server=%s' % user_agent)
drivers.append(webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe",options=chrome_options))
return drivers
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句