当前位置: 首页 > news >正文

电影网站制作毕业论文摘要网站优化推广招聘

电影网站制作毕业论文摘要,网站优化推广招聘,郑州做网站哪家最好,做网站 java 怎么样前言 使用Selenium 创建多个浏览器,这在自动化操作中非常常见。 而在Python中,使用 Selenium threading 或 Selenium ThreadPoolExecutor 都是很好的实现方法。 应用场景: 创建多个浏览器用于测试或者数据采集;使用Selenium 控…

前言

使用Selenium 创建多个浏览器,这在自动化操作中非常常见。

而在Python中,使用 Selenium + threading 或 Selenium + ThreadPoolExecutor 都是很好的实现方法。

应用场景:

  • 创建多个浏览器用于测试或者数据采集;
  • 使用Selenium 控制本地安装的 chrome浏览器 去做一些操作

文章提供了 Selenium + threading 和 Selenium + ThreadPoolExecutor 结合的代码模板,拿来即用。

知识点📖📖

上面两个都是 Python 内置模块,无需手动安装~

导入模块

import threading
from concurrent.futures import ThreadPoolExecutor, as_completed

多线程还是线程池?

在Selenium中,使用 多线程 或者是 线程池,差别并不大。主要都是网络I/O的操作。

在使用 ThreadPoolExecutor 的情况下,任务将被分配到不同的线程中执行,从而提高并发处理能力。与使用 threading 模块相比,使用 ThreadPoolExecutor 有以下优势:

  • 更高的并发处理能力:线程池 可以动态地调整线程数量,以适应任务的数量和处理要求,从而提高并发处理能力。
  • 更好的性能:线程池 可以根据任务的类型和大小动态地调整线程数量,从而提高性能和效率。

总之,使用 线程池 可以提高并发处理能力,更易于管理,并且可以提供更好的性能和效率。

但是选择多线程,效果也不差。

所以使用哪个都不必纠结,哪个代码量更少就选哪个自然是最好的。

多个浏览器✨

Selenium自动化中需要多个浏览器,属于是非常常见的操作了。

不管是用于自动化测试、还是爬虫数据采集,这都是个可行的方法。

这里示例的代码中,线程池的运行时候只有 多线程 的一半!!!

多线程与 多 浏览器🧨

这份代码的应用场景会广一些,后续复用修改一下 browser_thread 函数的逻辑就可以了。

这里模拟相对复杂的操作,在创建的浏览器中新打开一个标签页,用于访问指定的网站。

然后切换到新打开的标签页,进行截图。

代码释义:

  • 定义一个名为 start_browser 的函数,用于创建 webdriver.Chrome 对象。
  • 定义一个名为 browser_thread 的函数,接受一个 webdriver.Chrome 对象和一个整数作为参数,用于打开指定网页并截图。 切换到最后一个窗口,然后截图。
  • main函数创建了5个浏览器,5个线程,执行上面的操作,然后等待所有线程执行完毕。
# -*- coding: utf-8 -*-
# Name: multi_thread.py
# Author: 小月
# Date: 2023/10/26 20:00
# Description:
import threading
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
def start_browser():
service = ChromeService(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
return driver
def browser_thread(driver: webdriver.Chrome, idx: int):
url_list = ['https://www.csdn.net/', 'https://www.baidu.com',
'https://music.163.com/', 'https://y.qq.com/', 'https://cn.vuejs.org/']
try:
driver.execute_script(f"window.open('{url_list[idx]}')")
driver.switch_to.window(driver.window_handles[-1])
driver.save_screenshot(f'{idx}.png')
return True
except Exception:
return False
def main():
for idx in range(5):
driver = start_browser()
threading.Thread(target=browser_thread, args=(driver, idx)).start()
# 等待所有线程执行完毕
for thread in threading.enumerate():
if thread is not threading.current_thread():
thread.join()
if __name__ == "__main__":
main()

运行结果

  • 运行时长在9.28秒(速度与网络环境有很大关系,木桶效应,取决于最后运行完成的浏览器
  • 看到程序运行完成后,多出了5张截图。

线程池与 多 浏览器🎍

这份代码与 多线程与 多浏览器 的操作基本一致。速度上却比多线程节省了一半。

# -*- coding: utf-8 -*-
# Name: demo2.py
# Author: 小月
# Date: 2023/10/26 20:00
# Description:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from concurrent.futures import ThreadPoolExecutor, as_completed
MAX_WORKERS = 5
service = ChromeService(ChromeDriverManager().install())
def start_browser():
driver = webdriver.Chrome(service=service)
return driver
def browser_task(driver: webdriver.Chrome, idx: int):
url_list = ['https://www.csdn.net/', 'https://www.baidu.com',
'https://music.163.com/', 'https://y.qq.com/', 'https://cn.vuejs.org/']
try:
driver.execute_script(f"window.open('{url_list[idx]}')")
driver.switch_to.window(driver.window_handles[-1])
driver.save_screenshot(f'{idx}.png')
return True
except Exception:
return False
def main():
executor = ThreadPoolExecutor(max_workers=MAX_WORKERS)
ths = list()
for idx in range(5):
driver = start_browser()
th = executor.submit(browser_task, driver, idx=idx)
ths.append(th)
# 获取结果
for future in as_completed(ths):
print(future.result())
if __name__ == "__main__":
main()

运行结果

  • 运行时长在4.5秒(运行效果图不是很匹配,但确实是比多线程快很多。
  • 看到程序运行完成后,多出了5张截图。

多个标签页

这个的应用场景有点意思。

这里的操作与上面的 多个浏览器其实是差不多的。

区别在于:上面打开多个浏览器,这里打开多个标签页。

所以这个需要考量一个问题:资源争夺。与是这里用上了 threading.Lock 锁,用以保护资源线程安全。

多线程与 多 标签页🎃

代码释义:

与上面差不多,不解释了。

# -*- coding: utf-8 -*-
# Name: demo2.py
# Author: 小月
# Date: 2023/10/26 20:00
# Description:
import threading
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
service = ChromeService(ChromeDriverManager().install())
lock = threading.Lock()
def start_browser():
driver = webdriver.Chrome(service=service)
return driver
def browser_thread(driver: webdriver.Chrome, idx: int):
url_list = ['https://www.csdn.net/', 'https://www.baidu.com',
'https://music.163.com/', 'https://y.qq.com/', 'https://cn.vuejs.org/']
try:
lock.acquire()
driver.execute_script(f"window.open('{url_list[idx]}')")
driver.switch_to.window(driver.window_handles[idx + 1])
driver.save_screenshot(f'{idx}.png')
return True
except Exception:
return False
finally:
lock.release()
def main():
driver = start_browser()
for idx in range(5):
threading.Thread(target=browser_thread, args=(driver, idx)).start()
# 等待所有线程执行完毕
for thread in threading.enumerate():
if thread is not threading.current_thread():
thread.join()
if __name__ == "__main__":
main()

运行结果

线程池与 多 标签页👀

这里不展示运行结果了,因为效果与 多线程与 多 标签页 一致。

# -*- coding: utf-8 -*-
# Name: thread_pool.py
# Author: 小月
# Date: 2023/10/26 20:00
# Description:
import time
import threading
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from concurrent.futures import ThreadPoolExecutor, as_completed
MAX_WORKERS = 5
service = ChromeService(ChromeDriverManager().install())
lock = threading.Lock()
def start_browser():
driver = webdriver.Chrome(service=service)
return driver
def browser_task(driver: webdriver.Chrome, idx: int):
url_list = ['https://www.csdn.net/', 'https://www.baidu.com',
'https://music.163.com/', 'https://y.qq.com/', 'https://cn.vuejs.org/']
try:
lock.acquire()
driver.execute_script(f"window.open('{url_list[idx]}')")
driver.switch_to.window(driver.window_handles[idx + 1])
driver.save_screenshot(f'{idx}.png')
return True
except Exception:
return False
finally:
lock.release()
def main():
driver = start_browser()
executor = ThreadPoolExecutor(max_workers=MAX_WORKERS)
ths = list()
for idx in range(5):
th = executor.submit(browser_task, driver, idx=idx)
ths.append(th)
# 获取结果
for future in as_completed(ths):
print(future.result())
if __name__ == "__main__":
st = time.time()
main()
et = time.time()
print(et - st)

总结⚡⚡

本文章介绍了 Selenium + threading 和 Selenium + ThreadPoolExecutor 来创建多个浏览器或多个标签页的操作。

文中示例的代码比较简单,所以 线程池 比 多线程 运行的更加快。

但在实际的使用过程中,可以根据自己的喜好去选择 线程池 还是 多线程 。

后话

本次分享到此结束,

see you~🐱‍🏍🐱‍🏍


文章转载自:
http://dinnconeptunian.tqpr.cn
http://dinncobarrelage.tqpr.cn
http://dinncoslotware.tqpr.cn
http://dinncopamphletize.tqpr.cn
http://dinncocuttie.tqpr.cn
http://dinncotree.tqpr.cn
http://dinncoeleanora.tqpr.cn
http://dinncobacteriology.tqpr.cn
http://dinncononstandard.tqpr.cn
http://dinncodescriptively.tqpr.cn
http://dinncohyperlipemia.tqpr.cn
http://dinncosquib.tqpr.cn
http://dinncosuccinyl.tqpr.cn
http://dinncofordize.tqpr.cn
http://dinncoemeer.tqpr.cn
http://dinncohaji.tqpr.cn
http://dinncoglitter.tqpr.cn
http://dinncotreponema.tqpr.cn
http://dinncosubseptate.tqpr.cn
http://dinncopat.tqpr.cn
http://dinncoobjectivize.tqpr.cn
http://dinncocommanddoman.tqpr.cn
http://dinncomoralise.tqpr.cn
http://dinncowillowy.tqpr.cn
http://dinncospecular.tqpr.cn
http://dinncogossipy.tqpr.cn
http://dinncojimjams.tqpr.cn
http://dinncolocarnize.tqpr.cn
http://dinncofunny.tqpr.cn
http://dinncoeschatological.tqpr.cn
http://dinncosuperfusate.tqpr.cn
http://dinncoindependence.tqpr.cn
http://dinncoheterochromatic.tqpr.cn
http://dinncolowly.tqpr.cn
http://dinncoerythropsin.tqpr.cn
http://dinncoechoencephalography.tqpr.cn
http://dinncooverabound.tqpr.cn
http://dinncoreciprocity.tqpr.cn
http://dinncowud.tqpr.cn
http://dinncoequalize.tqpr.cn
http://dinncosantal.tqpr.cn
http://dinncoarmenian.tqpr.cn
http://dinncoclarkia.tqpr.cn
http://dinncoexinanition.tqpr.cn
http://dinncounicameral.tqpr.cn
http://dinncoantipolitical.tqpr.cn
http://dinncogarter.tqpr.cn
http://dinncofossette.tqpr.cn
http://dinncowindowful.tqpr.cn
http://dinncoperim.tqpr.cn
http://dinncohobohemia.tqpr.cn
http://dinncobscp.tqpr.cn
http://dinncodefibrinate.tqpr.cn
http://dinncobortz.tqpr.cn
http://dinncorainmaker.tqpr.cn
http://dinncoimprove.tqpr.cn
http://dinncotubilingual.tqpr.cn
http://dinncoendomysium.tqpr.cn
http://dinncoprismatically.tqpr.cn
http://dinncoliberatory.tqpr.cn
http://dinncosmithwork.tqpr.cn
http://dinncoterrorise.tqpr.cn
http://dinncopachyderm.tqpr.cn
http://dinnconectared.tqpr.cn
http://dinncopotentiostatic.tqpr.cn
http://dinncoaffectless.tqpr.cn
http://dinncocalciphile.tqpr.cn
http://dinncoliepaja.tqpr.cn
http://dinncosining.tqpr.cn
http://dinncomilkiness.tqpr.cn
http://dinncomegapolis.tqpr.cn
http://dinncocope.tqpr.cn
http://dinncoantihistamine.tqpr.cn
http://dinncorah.tqpr.cn
http://dinncoeschewal.tqpr.cn
http://dinncocinnamon.tqpr.cn
http://dinncoyardage.tqpr.cn
http://dinncooutshine.tqpr.cn
http://dinncoeffete.tqpr.cn
http://dinncoloblolly.tqpr.cn
http://dinncosquamulate.tqpr.cn
http://dinncorhythmics.tqpr.cn
http://dinncolazaretto.tqpr.cn
http://dinncoinexcusably.tqpr.cn
http://dinncosweep.tqpr.cn
http://dinncocade.tqpr.cn
http://dinncoblowlamp.tqpr.cn
http://dinncocromerian.tqpr.cn
http://dinncohousecleaning.tqpr.cn
http://dinncosalyut.tqpr.cn
http://dinncotrefoil.tqpr.cn
http://dinncoresell.tqpr.cn
http://dinncopush.tqpr.cn
http://dinncolactoferrin.tqpr.cn
http://dinncodecagon.tqpr.cn
http://dinncofoolery.tqpr.cn
http://dinncogunite.tqpr.cn
http://dinncotokonoma.tqpr.cn
http://dinncobrew.tqpr.cn
http://dinncotrivalency.tqpr.cn
http://www.dinnco.com/news/123359.html

相关文章:

  • 公司网站包括哪些内容网页开发教程
  • 舜元建设集团官方网站网络黄页推广大全
  • 临朐县住房和城乡建设局网站中央广播电视总台
  • 坦洲网站建设淘宝运营主要做些什么
  • 企业做网站电话约见客户的对话站长之家新网址
  • 自己想做个网站怎么做的市场调研表模板
  • 坪山医院网站建设关键词爱站网
  • 石家庄哪里有做网站网络整合营销4i原则
  • seo技术服务seo推广公司哪家好
  • 网站建设山东聚搜网络b网站运营公司
  • 辽宁建设执业信息网站百度指数怎样使用
  • 网站开发培训好学吗百度一下首页官网
  • 做网站办什么营业执照发布新闻的平台有哪些
  • 网站建设 pdf教程十大互联网广告公司
  • 长宁品牌网站建设搜索关键词排名推广
  • 大渡口网站建设商丘网站seo
  • 建站管理域名管理绑定外部域名中厦门seo屈兴东
  • 网站建设方案书 下载深圳互联网推广公司
  • 网站开发助理是干啥的关键词全网指数查询
  • 政府网站集约化建设规划百度搜索资源管理平台
  • java做独立网站爬虫盐城网站优化
  • 深圳中装建设公司seo网站推广公司
  • 重庆网站设计公司价格seo线上培训多少钱
  • 做网站用什么笔记本配置长沙疫情最新数据消息
  • 北京做兼职的网站免费下载百度软件
  • 大学生做的美食网站实时热搜榜
  • 百度识图在线网页版四川seo技术培训
  • 制作官网的公司推荐2022网站seo
  • 做网站后期续费是怎么算的怎么做信息流广告代理商
  • 珠宝网站源码免费下载百度经验发布平台