【发布时间】:2020-11-18 22:33:02
【问题描述】:
当我将代理变量放在 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)
为了解释我在做什么,我在做一个同时打开多个窗口并且每个窗口都有自己唯一代理的shoe bot,我想将代理变量作为参数放在函数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
【问题讨论】:
标签: python python-3.x list for-loop