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