我正在尝试为列表中的每个项目创建多个chrome线程,并同时为列表中的每个项目执行功能,但是不知道从哪里开始任何帮助将不胜感激。
代码段
import sys def spotify(elem1, elem2, elem3): print("proxy: {}, cc: {}, cvc: {}".format(elem1, elem2, elem3)) def get_cc(): cc = ['5136154545452522', '51365445452823', '51361265424522'] return cc def get_cvc(): cvc = ['734', '690', '734'] return cvc def get_proxies(): proxies = ['51.77.545.171:8080', '51.77.254.171:8080', '51.77.258.82:8080'] return proxies proxArr = get_proxies() ccArr = get_cc() cvcArr = get_cvc() yeslist = ['y','yes'] for elem in zip(proxArr, ccArr, cvcArr): spotify(elem[0], elem[1], elem[2]) restart=input("Do you wish to start again: ").lower() if restart not in yeslist: sys.exit("Exiting")
与这里的答案类似,您可以启动多个Chrome浏览器线程。
execute_chrome
Thread
args=(elem, )
my_selenium_tests.py
最好从命令行而不是从交互式环境(例如Jupyter笔记本)运行脚本
from selenium import webdriver
import threading import random import time
number_of_threads = 4
def execute_chrome(url): chrome = webdriver.Chrome() chrome.get(url) time.sleep(1 + random.random() * 5) driver.quit()
urls = ('https://www.google.com’, 'https://www.bing.com’, 'https://www.duckduckgo.com’, 'https://www.yahoo.com’)
threads = [] for i in range(number_of_threads): t = threading.Thread(target=execute_chrome, args=(urls[i], )) t.start() threads.append(t)
for thread in threads: thread.join()