【问题标题】:Selenium create multiple chrome threads for each item in array(list) and execute function simultaneouslySelenium 为数组(列表)中的每个项目创建多个 chrome 线程并同时执行函数
【发布时间】:2019-05-10 20:21:58
【问题描述】:

我正在尝试为列表中的每个项目创建多个 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")

【问题讨论】:

  • 这个帖子回答了你的问题吗? stackoverflow.com/questions/49617485/…
  • 不,我没有尝试过那里的解决方案,但我收到了这个错误AttributeError: module 'threading' has no attribute 'RLock'
  • edit 我已经命名了 python 文件线程并得到了这个错误我现在已经改变了它并且来自链接的代码不会产生任何错误但它只运行一个 chrome

标签: python python-3.x selenium selenium-chromedriver


【解决方案1】:

类似答案here,可以启动Chrome的多线程。

  • 定义一个执行 Selenium 代码的函数,在本例中为 execute_chrome
  • 将所有必需的参数添加到函数定义中
  • Thread 调用中将参数作为元组传递,例如args=(elem, )
  • 使用与另一个 Python 包不同的名称保存脚本,例如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()
    

【讨论】:

  • 非常感谢,因为您稍后发送的所有链接都很好:) 问题是我已将 python 脚本命名为 threading.py
猜你喜欢
  • 2014-07-24
  • 1970-01-01
  • 2017-11-18
  • 1970-01-01
  • 1970-01-01
  • 2020-01-17
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多