selenium 优化 提升性能
结果:
用时:7.200437545776367s
用时:5.909301519393921s headless
用时:4.924464702606201s headless\phone
用时:4.935844421386719s headless\phone\no image
为了测试图片,http://image.baidu.com/
事实证明,no image对打开网页没影响,对网络有影响吧,这个有点
1 from selenium import webdriver 2 import time 3 4 5 def method0(): 6 s1 = time.time() 7 options = webdriver.ChromeOptions() 8 options.binary_location = r'd:\downloads\app\chrome\chrome.exe' 9 browser = webdriver.Chrome(options=options) 10 browser.get('http://image.baidu.com/') 11 browser.save_screenshot('baidu.png') 12 browser.quit() 13 s2 = time.time() 14 print('用时:{0}s'.format(s2 - s1)) 15 16 17 def method1(): 18 # 优化1 headless 19 s1 = time.time() 20 options = webdriver.ChromeOptions() 21 options.binary_location = r'd:\downloads\app\chrome\chrome.exe' 22 options.headless = True 23 browser = webdriver.Chrome(options=options) 24 browser.get('http://image.baidu.com/') 25 browser.save_screenshot('baidu_headless.png') 26 browser.quit() 27 s2 = time.time() 28 print('用时:{0}s headless'.format(s2 - s1)) 29 30 31 def method2(): 32 # 优化2 headless\phone 33 s1 = time.time() 34 options = webdriver.ChromeOptions() 35 options.add_argument('user-agent={0}'.format('MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1')) 36 options.binary_location = r'd:\downloads\app\chrome\chrome.exe' 37 options.headless = True 38 browser = webdriver.Chrome(options=options) 39 browser.get('http://image.baidu.com/') 40 browser.save_screenshot('baidu_headless_phone.png') 41 browser.quit() 42 s2 = time.time() 43 print(r'用时:{0}s headless\phone'.format(s2 - s1)) 44 45 46 def method3(): 47 # 优化3 headless\phone\no image 48 s1 = time.time() 49 options = webdriver.ChromeOptions() 50 options.add_argument('user-agent={0}'.format('MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1')) 51 options.binary_location = r'd:\downloads\app\chrome\chrome.exe' 52 options.headless = True 53 options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2}) 54 browser = webdriver.Chrome(options=options) 55 browser.get('http://image.baidu.com/') 56 browser.save_screenshot('baidu_headless_phone_image.png') 57 browser.quit() 58 s2 = time.time() 59 print(r'用时:{0}s headless\phone\no image'.format(s2 - s1)) 60 61 62 if __name__ == '__main__': 63 method0() 64 method1() 65 method2() 66 method3()