翼度科技»论坛 编程开发 python 查看内容

玫瑰花变蚊子血,自动化无痕浏览器对比测试,新贵PlayWright Vs 老牌Selenium

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
也许每一个男子全都有过这样的两个女人,至少两个。娶了红玫瑰,久而久之,红的变了墙上的一抹蚊子血,白的还是床前明月光;娶了白玫瑰,白的便是衣服上沾的一粒饭黏子,红的却是心口上一颗朱砂痣。--张爱玲《红玫瑰与白玫瑰》
Selenium一直都是Python开源自动化浏览器工具的王者,但这两年微软开源的PlayWright异军突起,后来者居上,隐隐然有撼动Selenium江湖地位之势,本次我们来对比PlayWright与Selenium之间的差异,看看曾经的玫瑰花Selenium是否会变成蚊子血。
PlayWright的安装和使用

PlayWright是由业界大佬微软(Microsoft)开源的端到端 Web 测试和自动化库,可谓是大厂背书,功能满格,虽然作为无头浏览器,该框架的主要作用是测试 Web 应用,但事实上,无头浏览器更多的是用于 Web 抓取目的,也就是爬虫。
首先终端运行安装命令:
  1. pip3 install playwright
复制代码
程序返回:
  1. Successfully built greenlet  
  2. Installing collected packages: pyee, greenlet, playwright  
  3.   Attempting uninstall: greenlet  
  4.     Found existing installation: greenlet 2.0.2  
  5.     Uninstalling greenlet-2.0.2:  
  6.       Successfully uninstalled greenlet-2.0.2  
  7. Successfully installed greenlet-2.0.1 playwright-1.30.0 pyee-9.0.4
复制代码
目前最新稳定版为1.30.0
随后可以选择直接安装浏览器驱动:
  1. playwright install
复制代码
程序返回:
  1. Downloading Chromium 110.0.5481.38 (playwright build v1045) from https://playwright.azureedge.net/builds/chromium/1045/chromium-mac-arm64.zip  
  2. 123.8 Mb [====================] 100% 0.0s  
  3. Chromium 110.0.5481.38 (playwright build v1045) downloaded to /Users/liuyue/Library/Caches/ms-playwright/chromium-1045  
  4. Downloading FFMPEG playwright build v1008 from https://playwright.azureedge.net/builds/ffmpeg/1008/ffmpeg-mac-arm64.zip  
  5. 1 Mb [====================] 100% 0.0s  
  6. FFMPEG playwright build v1008 downloaded to /Users/liuyue/Library/Caches/ms-playwright/ffmpeg-1008  
  7. Downloading Firefox 108.0.2 (playwright build v1372) from https://playwright.azureedge.net/builds/firefox/1372/firefox-mac-11-arm64.zip  
  8. 69.8 Mb [====================] 100% 0.0s  
  9. Firefox 108.0.2 (playwright build v1372) downloaded to /Users/liuyue/Library/Caches/ms-playwright/firefox-1372  
  10. Downloading Webkit 16.4 (playwright build v1767) from https://playwright.azureedge.net/builds/webkit/1767/webkit-mac-12-arm64.zip  
  11. 56.9 Mb [====================] 100% 0.0s  
  12. Webkit 16.4 (playwright build v1767) downloaded to /Users/liuyue/Library/Caches/ms-playwright/webkit-1767
复制代码
默认会下载Chromium内核、Firefox以及Webkit驱动。
其中使用最广泛的就是基于Chromium内核的浏览器,最负盛名的就是Google的Chrome和微软自家的Edge。
确保当前电脑安装了Edge浏览器,让我们小试牛刀一把:
  1. from playwright.sync_api import sync_playwright  
  2. import time  
  3. with sync_playwright() as p:  
  4.     browser = p.chromium.launch(channel="msedge", headless=True)  
  5.     page = browser.new_page()  
  6.     page.goto('http:/v3u.cn')  
  7.     page.screenshot(path=f'./example-v3u.png')
  8.     time.sleep(5)
  9.     browser.close()
复制代码
这里导入sync_playwright模块,顾名思义,同步执行,通过上下文管理器开启浏览器进程。
随后通过channel指定edge浏览器,截图后关闭浏览器进程:

我们也可以指定headless参数为True,让浏览器再后台运行:
  1. from playwright.sync_api import sync_playwright  
  2. with sync_playwright() as p:  
  3.     browser = p.chromium.launch(channel="msedge", headless=True)  
  4.     page = browser.new_page()  
  5.     page.goto('http:/v3u.cn')  
  6.     page.screenshot(path=f'./example-v3u.png')  
  7.     browser.close()
复制代码
除了同步模式,PlayWright也支持异步非阻塞模式:
  1. import asyncio  
  2. from playwright.async_api import async_playwright  
  3.   
  4. async def main():  
  5.     async with async_playwright() as p:  
  6.         browser = await p.chromium.launch(channel="msedge", headless=False)  
  7.         page = await browser.new_page()  
  8.         await page.goto("http://v3u.cn")  
  9.         print(await page.title())  
  10.         await browser.close()  
  11.   
  12. asyncio.run(main())
复制代码
可以通过原生协程库asyncio进行调用,PlayWright内置函数只需要添加await关键字即可,非常方便,与之相比,Selenium原生库并不支持异步模式,必须安装三方扩展才可以。
最炫酷的是,PlayWright可以对用户的浏览器操作进行录制,并且可以转换为相应的代码,在终端执行以下命令:
  1. python -m playwright codegen --target python -o 'edge.py' -b chromium --channel=msedge
复制代码
这里通过codegen命令进行录制,指定浏览器为edge,将所有操作写入edge.py的文件中:

与此同时,PlayWright也支持移动端的浏览器模拟,比如苹果手机:
  1. from playwright.sync_api import sync_playwright  
  2. with sync_playwright() as p:  
  3.     iphone_13 = p.devices['iPhone 13 Pro']  
  4.     browser = p.webkit.launch(headless=False)  
  5.     page = browser.new_page()  
  6.     page.goto('https://v3u.cn')  
  7.     page.screenshot(path='./v3u-iphone.png')  
  8.     browser.close()
复制代码
这里模拟Iphone13pro的浏览器访问情况。
当然了,除了UI功能测试,我们当然还需要PlayWright帮我们干点脏活累活,那就是爬虫:
  1. from playwright.sync_api import sync_playwright   
  2.    
  3. def extract_data(entry):   
  4.         name = entry.locator("h3").inner_text().strip("\n").strip()   
  5.         capital = entry.locator("span.country-capital").inner_text()   
  6.         population = entry.locator("span.country-population").inner_text()   
  7.         area = entry.locator("span.country-area").inner_text()   
  8.    
  9.         return {"name": name, "capital": capital, "population": population, "area (km sq)": area}   
  10.    
  11. with sync_playwright() as p:   
  12.         # launch the browser instance and define a new context   
  13.         browser = p.chromium.launch()   
  14.         context = browser.new_context()   
  15.         # open a new tab and go to the website   
  16.         page = context.new_page()   
  17.         page.goto("https://www.scrapethissite.com/pages/simple/")   
  18.         page.wait_for_load_state("load")   
  19.         # get the countries   
  20.         countries = page.locator("div.country")   
  21.         n_countries = countries.count()   
  22.    
  23.         # loop through the elements and scrape the data   
  24.         data = []   
  25.    
  26.         for i in range(n_countries):   
  27.                 entry = countries.nth(i)   
  28.                 sample = extract_data(entry)   
  29.                 data.append(sample)   
  30.    
  31. browser.close()
复制代码
这里data变量就是抓取的数据内容:
  1. [   
  2.         {'name': 'Andorra', 'capital': 'Andorra la Vella', 'population': '84000', 'area (km sq)': '468.0'},   
  3.         {'name': 'United Arab Emirates', 'capital': 'Abu Dhabi', 'population': '4975593', 'area (km sq)': '82880.0'},   
  4.         {'name': 'Afghanistan', 'capital': 'Kabul', 'population': '29121286', 'area (km sq)': '647500.0'},   
  5.         {'name': 'Antigua and Barbuda', 'capital': "St. John's", 'population': '86754', 'area (km sq)': '443.0'},   
  6.         {'name': 'Anguilla', 'capital': 'The Valley', 'population': '13254', 'area (km sq)': '102.0'},   
  7.         ...   
  8. ]
复制代码
基本上,该有的功能基本都有,更多功能请参见官方文档:https://playwright.dev/python/docs/library
Selenium

Selenium曾经是用于网络抓取和网络自动化的最流行的开源无头浏览器工具之一。在使用 Selenium 进行抓取时,我们可以自动化浏览器、与 UI 元素交互并在 Web 应用程序上模仿用户操作。Selenium 的一些核心组件包括 WebDriver、Selenium IDE 和 Selenium Grid。
关于Selenium的一些基本操作请移玉步至:python3.7爬虫:使用Selenium带Cookie登录并且模拟进行表单上传文件,这里不作过多赘述。
如同前文提到的,与Playwright相比,Selenium需要第三方库来实现异步并发执行,同时,如果需要录制动作视频,也需要使用外部的解决方案。
就像Playwright那样,让我们使用 Selenium 构建一个简单的爬虫脚本。
首先导入必要的模块并配置 Selenium 实例,并且通过设置确保无头模式处于活动状态option.headless = True:
  1. from selenium import webdriver   
  2. from selenium.webdriver.chrome.service import Service   
  3. from selenium.webdriver.common.by import By   
  4. # web driver manager: https://github.com/SergeyPirogov/webdriver_manager   
  5. # will help us automatically download the web driver binaries   
  6. # then we can use `Service` to manage the web driver's state.   
  7. from webdriver_manager.chrome import ChromeDriverManager   
  8.    
  9. def extract_data(row):   
  10.         name = row.find_element(By.TAG_NAME, "h3").text.strip("\n").strip()   
  11.         capital = row.find_element(By.CSS_SELECTOR, "span.country-capital").text   
  12.         population = row.find_element(By.CSS_SELECTOR, "span.country-population").text   
  13.         area = row.find_element(By.CSS_SELECTOR, "span.country-area").text   
  14.    
  15.         return {"name": name, "capital": capital, "population": population, "area (km sq)": area}   
  16.    
  17. options = webdriver.ChromeOptions()   
  18. options.headless = True   
  19. # this returns the path web driver downloaded   
  20. chrome_path = ChromeDriverManager().install()   
  21. # define the chrome service and pass it to the driver instance   
  22. chrome_service = Service(chrome_path)   
  23. driver = webdriver.Chrome(service=chrome_service, options=options)   
  24.    
  25. url = "https://www.scrapethissite.com/pages/simple"   
  26.    
  27. driver.get(url)   
  28. # get the data divs   
  29. countries = driver.find_elements(By.CSS_SELECTOR, "div.country")   
  30.    
  31. # extract the data   
  32. data = list(map(extract_data, countries))   
  33.    
  34. driver.quit()
复制代码
数据返回:
  1. [   
  2.         {'name': 'Andorra', 'capital': 'Andorra la Vella', 'population': '84000', 'area (km sq)': '468.0'},   
  3.         {'name': 'United Arab Emirates', 'capital': 'Abu Dhabi', 'population': '4975593', 'area (km sq)': '82880.0'},   
  4.         {'name': 'Afghanistan', 'capital': 'Kabul', 'population': '29121286', 'area (km sq)': '647500.0'},   
  5.         {'name': 'Antigua and Barbuda', 'capital': "St. John's", 'population': '86754', 'area (km sq)': '443.0'},   
  6.         {'name': 'Anguilla', 'capital': 'The Valley', 'population': '13254', 'area (km sq)': '102.0'},   
  7.         ...   
  8. ]
复制代码
性能测试

在数据抓取量一样的前提下,我们当然需要知道到底谁的性能更好,是PlayWright,还是Selenium?
这里我们使用Python3.10内置的time模块来统计爬虫脚本的执行速度。
PlayWright:
  1. import time   
  2. from playwright.sync_api import sync_playwright   
  3.    
  4. def extract_data(entry):   
  5.         name = entry.locator("h3").inner_text().strip("\n").strip()   
  6.         capital = entry.locator("span.country-capital").inner_text()   
  7.         population = entry.locator("span.country-population").inner_text()   
  8.         area = entry.locator("span.country-area").inner_text()   
  9.    
  10.         return {"name": name, "capital": capital, "population": population, "area (km sq)": area}   
  11.    
  12. start = time.time()   
  13. with sync_playwright() as p:   
  14.         # launch the browser instance and define a new context   
  15.         browser = p.chromium.launch()   
  16.         context = browser.new_context()   
  17.         # open a new tab and go to the website   
  18.         page = context.new_page()   
  19.         page.goto("https://www.scrapethissite.com/pages/")   
  20.         # click to the first page and wait while page loads   
  21.         page.locator("a[href='/pages/simple/']").click()   
  22.         page.wait_for_load_state("load")   
  23.         # get the countries   
  24.         countries = page.locator("div.country")   
  25.         n_countries = countries.count()   
  26.    
  27.         data = []   
  28.    
  29.         for i in range(n_countries):   
  30.                 entry = countries.nth(i)   
  31.                 sample = extract_data(entry)   
  32.                 data.append(sample)   
  33.    
  34. browser.close()   
  35. end = time.time()   
  36.    
  37. print(f"The whole script took: {end-start:.4f}")
复制代码
Selenium:
  1. import time   
  2. from selenium import webdriver   
  3. from selenium.webdriver.chrome.service import Service   
  4. from selenium.webdriver.common.by import By   
  5. # web driver manager: https://github.com/SergeyPirogov/webdriver_manager   
  6. # will help us automatically download the web driver binaries   
  7. # then we can use `Service` to manage the web driver's state.   
  8. from webdriver_manager.chrome import ChromeDriverManager   
  9.    
  10. def extract_data(row):   
  11.         name = row.find_element(By.TAG_NAME, "h3").text.strip("\n").strip()   
  12.         capital = row.find_element(By.CSS_SELECTOR, "span.country-capital").text   
  13.         population = row.find_element(By.CSS_SELECTOR, "span.country-population").text   
  14.         area = row.find_element(By.CSS_SELECTOR, "span.country-area").text   
  15.    
  16.         return {"name": name, "capital": capital, "population": population, "area (km sq)": area}   
  17.    
  18. # start the timer   
  19. start = time.time()   
  20.    
  21. options = webdriver.ChromeOptions()   
  22. options.headless = True   
  23. # this returns the path web driver downloaded   
  24. chrome_path = ChromeDriverManager().install()   
  25. # define the chrome service and pass it to the driver instance   
  26. chrome_service = Service(chrome_path)   
  27. driver = webdriver.Chrome(service=chrome_service, options=options)   
  28.    
  29. url = "https://www.scrapethissite.com/pages/"   
  30.    
  31. driver.get(url)   
  32. # get the first page and click to the link   
  33. first_page = driver.find_element(By.CSS_SELECTOR, "h3.page-title a")   
  34. first_page.click()   
  35. # get the data div and extract the data using beautifulsoup   
  36. countries_container = driver.find_element(By.CSS_SELECTOR, "section#countries div.container")   
  37. countries = driver.find_elements(By.CSS_SELECTOR, "div.country")   
  38.    
  39. # scrape the data using extract_data function   
  40. data = list(map(extract_data, countries))   
  41.    
  42. end = time.time()   
  43.    
  44. print(f"The whole script took: {end-start:.4f}")   
  45.    
  46. driver.quit()
复制代码
测试结果:

Y轴是执行时间,一望而知,Selenium比PlayWright差了大概五倍左右。
红玫瑰还是白玫瑰?

不得不承认,Playwright 和 Selenium 都是出色的自动化无头浏览器工具,都可以完成爬虫任务。我们还不能断定那个更好一点,所以选择那个取决于你的网络抓取需求、你想要抓取的数据类型、浏览器支持和其他考虑因素:
Playwright 不支持真实设备,而 Selenium 可用于真实设备和远程服务器。
Playwright 具有内置的异步并发支持,而 Selenium 需要第三方工具。
Playwright 的性能比 Selenium 高。
Selenium 不支持详细报告和视频录制等功能,而 Playwright 具有内置支持。
Selenium 比 Playwright 支持更多的浏览器。
Selenium 支持更多的编程语言。
结语

如果您看完了本篇文章,那么到底谁是最好的无头浏览器工具,答案早已在心间,所谓强中强而立强,只有弱者才害怕竞争,相信PlayWright的出现会让Selenium变为更好的自己,再接再厉,再创辉煌。

来源:https://www.cnblogs.com/v3ucn/p/17150357.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具