前言
對于網(wǎng)上流傳的提高selenium的方法有很多,但從程序運行的感官上來看,感覺提速并不明顯。所以,我特地做了一些測試,尋找最優(yōu)方案。
但是,由于對不同組的測試次數(shù)并不相同,結(jié)果可能不太嚴謹。但是,一般來說,結(jié)果基本都在一定范圍內(nèi)浮動,所以結(jié)果和多次測試大差不差。
0. 測速結(jié)果
1. Request發(fā)起請求
import time
import requests
t1 = time.time()
resp = requests.get('http://androil.sinaapp.com/modelyh/21177.html')
t2 = time.time()
print(f"一共耗時{t2-t1}")
測試結(jié)果:
一共耗時0.8379917144775391
一共耗時0.8982553482055664
一共耗時0.8139898777008057
2. Selenium發(fā)起請求(沒有添加任何配置)
import time
from selenium.webdriver import Edge
web = Edge()創(chuàng)建瀏覽器
t1 = time.time()
web.get("http://www.baidu.com")
t2 = time.time()
print(f"一共耗時{t2-t1}")
web.quit()
測試結(jié)果:
一共耗時2.5213799476623535
一共耗時2.655221462249756
一共耗時3.2438957691192627
一共耗時2.867304801940918
3. Selenium發(fā)起請求(禁用js、css、圖片加載,啟用無頭瀏覽器,禁用gpu,禁用插件)
import time
from selenium.webdriver import Edge
from selenium.webdriver.edge.options import Options
edge_option = Options()
prefs = {
'profile.default_content_setting_values': {
'images': 2, 無圖模式
'javascript': 2, 禁止腳本加載
'permissions.default.stylesheet': 2, 禁用css
}
}
edge_option.add_experimental_option('prefs', prefs) 添加配置
edge_option.add_argument('--headless') 無頭瀏覽器
edge_option.add_argument('--disable-plugins') 禁用插件
edge_option.add_argument("--disable--gpu")禁用顯卡
web = Edge(options=edge_option)創(chuàng)建瀏覽器
t1 = time.time()
web.get("http://www.baidu.com")
t2 = time.time()
print(f"一共耗時{t2-t1}")
web.quit()
測試結(jié)果:
一共耗時2.646573781967163
一共耗時3.0973236560821533
一共耗時1.9942853450775146
一共耗時4.202911376953125
**猜想:**效果不明顯,可能是瀏覽器打開的速度太慢了,直接使用已經(jīng)打開的瀏覽器
4. Selenium發(fā)起請求(在已打開的瀏覽器上發(fā)起請求)
import time
from selenium.webdriver import Edge
from selenium.webdriver.edge.options import Options
edge_option = Options()
edge_option.add_experimental_option("debuggerAddress","127.0.0.1:9222")
web = Edge(options=edge_option)創(chuàng)建瀏覽器
t1 = time.time()
web.get("http://www.baidu.com")
t2 = time.time()
print(f"一共耗時{t2-t1}")
web.quit()
測試結(jié)果:
一共耗時0.9570584297180176
一共耗時0.8212602138519287
一共耗時0.6052114963531494
一共耗時0.6656308174133301
一共耗時0.8359296321868896
這里的加載速度明顯快了很多
5. Selenium發(fā)起請求(在已打開的瀏覽器上發(fā)起請求,添加以上配置)
import time
from selenium.webdriver import Edge
from selenium.webdriver.edge.options import Options
edge_option = Options()
edge_option.add_argument('--blink-settings=imagesEnabled=false')
edge_option.add_argument('--headless') 無頭瀏覽器
edge_option.add_argument('–-disable-javascript') 禁用javascript
edge_option.add_argument('--disable-plugins') 禁用插件
edge_option.add_argument("--disable--gpu")禁用顯卡
edge_option.add_argument("--disable-images")禁用圖像
edge_option.add_experimental_option("debuggerAddress","127.0.0.1:9222")
web = Edge(options=edge_option)創(chuàng)建瀏覽器
t1 = time.time()
web.get("http://androil.sinaapp.com/modelyh/21177.html")
t2 = time.time()
print(f"一共耗時{t2-t1}")
web.quit()
測試結(jié)果:
一共耗時0.602916955947876
一共耗時0.6741578578948975
一共耗時0.7092258930206299
一共耗時0.6321258544921875
一共耗時0.682152509689331
加載速度快了一點,但是但是但是,在運行啟動瀏覽器的時候,可以明顯感覺到耗費的時間不只是零點幾秒。
**問題:**在計算時間的時候,我并沒有把創(chuàng)建對象的時間加進去
6. Selenium啟動(無配置)[瀏覽器對象創(chuàng)建耗時測試]
import time
from selenium.webdriver import Edge
t1 = time.time()
web = Edge()創(chuàng)建瀏覽器
web.get("http://www.baidu.com")
t2 = time.time()
print(f"一共耗時{t2-t1}")
web.quit()
測試結(jié)果:
一共耗時4.975956678390503
一共耗時5.392338275909424
一共耗時5.371351480484009
一共耗時5.267101287841797
一共耗時4.5134382247924805
一共耗時5.200875520706177
一共耗時4.3331522941589355
一共耗時4.416335582733154
總的來說,對于創(chuàng)建瀏覽器對象所耗費的時間,我們是無法縮短的。所以,在一個進程中,我們盡量只創(chuàng)建一個瀏覽器對象
6.1 Selenium啟動(無配置)[瀏覽器對象創(chuàng)建耗時測試][二次請求]
import time
from selenium.webdriver import Edge
from selenium.webdriver.edge.options import Options
edge_option = Options()
edge_option.add_experimental_option("debuggerAddress","127.0.0.1:9222")
web = Edge(options=edge_option)創(chuàng)建瀏覽器
web.get("https://www.sougou.com/")
t1 = time.time()
web.get('https://www.baidu.com/')
t2 = time.time()
print(f"二次請求一共耗時{t2-t1}")
web.quit()
測試結(jié)果:
二次請求一共耗時0.6411991119384766
二次請求一共耗時0.5332534313201904
二次請求一共耗時0.5351331233978271
二次請求一共耗時0.6386752128601074
7. Selenium啟動已經(jīng)打開的瀏覽器(無配置)[瀏覽器對象創(chuàng)建耗時測試]
import time
from selenium.webdriver import Edge
from selenium.webdriver.edge.options import Options
edge_option = Options()
edge_option.add_experimental_option("debuggerAddress","127.0.0.1:9222")
t1 = time.time()
web = Edge(options=edge_option)創(chuàng)建瀏覽器
web.get("http://androil.sinaapp.com/modelyh/21177.html")
t2 = time.time()
print(f"一共耗時{t2-t1}")
web.quit()
測試結(jié)果:
一共耗時3.3087267875671387
一共耗時3.16994047164917
一共耗時3.21472430229187
一共耗時3.1717209815979004
一共耗時3.227937698364258
速度有所提升,但還不夠快
這里需要知道的是,由于瀏覽器已經(jīng)打開,我們是無法給已啟動的瀏覽器添加配置的,所有需要我們?nèi)ナ謩优渲谩?/strong>
不信看下面
8. Selenium啟動(有配置,但是在代碼中)[瀏覽器對象創(chuàng)建耗時測試]
import time
from selenium.webdriver import Edge
from selenium.webdriver.edge.options import Options
edge_option = Options()
edge_option.add_argument('--blink-settings=imagesEnabled=false')
edge_option.add_argument('--headless') 無頭瀏覽器
edge_option.add_argument('–-disable-javascript') 禁用javascript
edge_option.add_argument('--disable-plugins') 禁用插件
edge_option.add_argument("--disable--gpu")禁用顯卡
edge_option.add_argument("--disable-images")禁用圖像
edge_option.add_experimental_option("debuggerAddress","127.0.0.1:9222")
t1 = time.time()
web = Edge(options=edge_option)創(chuàng)建瀏覽器
web.get("https://www.baidu.com/")
t2 = time.time()
print(f"一共耗時{t2-t1}")
web.quit()
測試結(jié)果:文章來源:http://www.zghlxwxcb.cn/news/detail-770777.html
一共耗時3.41314959526062
一共耗時3.263237714767456
一共耗時3.3738415241241455
一共耗時3.182044267654419
一共耗時3.2655630111694336
9. Selenium啟動已打開的瀏覽器(在瀏覽器中配置)
測試結(jié)果:文章來源地址http://www.zghlxwxcb.cn/news/detail-770777.html
一共耗時3.175990343093872
一共耗時3.1182053089141846
一共耗時3.0257885456085205
一共耗時3.1209449768066406
結(jié)論
- 使用已經(jīng)打開的瀏覽器
- 對瀏覽器進行配置
- 只創(chuàng)建一個瀏覽器對象
到了這里,關(guān)于Selenium | 你真的知道怎么提高Selenium的運行速度嗎?的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!