gpt4 book ai didi

python - 在 Python 中同时执行多个任务

转载 作者:太空宇宙 更新时间:2023-11-04 01:15:51 24 4
gpt4 key购买 nike

以下代码一次可以从一个给定的 url 下载一个文件:

from selenium import webdriver

with open("url_lists.txt","r") as fi: ###The text file contains hundreds of urls
urls = fi.read().splitlines()

for url in urls:
browser = webdriver.Firefox()
browser.get(url)
browser.find_element_by_id('download').click()

我想修改代码,让5个不同的浏览器同时打开5个url,同时下载5个文件。

我怎样才能完成它?

最佳答案

您可以使用线程

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from selenium import webdriver
from threading import Thread

with open("url_lists.txt","r") as fi: ###The text file contains hundreds of urls
urls = fi.read().splitlines()

def func(url, bro):

browserFunc = getattr(webdriver, bro, webdriver.Firefox)
browser = browserFunc()
browser.get(url)
browser.find_element_by_id('download').click()

t = []
urls = [1,2,3,4,5]
bros = [1,2,3,4,5]

for i in range(len(urls)):
t.append(Thread(target=func, args=[urls[i], bros[i]]))
for i in t:
t.start()
for i in t:
t.join()


if __name__ == '__main__':

a = test1()

关于python - 在 Python 中同时执行多个任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24776061/

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