python+selenium+Chrome options參數(shù)的使用
Chrome Options常用的行為一般有以下幾種:
- 禁止圖片和視頻的加載:提升網(wǎng)頁加載速度。
- 添加代理:用于翻墻訪問某些頁面,或者應(yīng)對IP訪問頻率限制的反爬技術(shù)。
- 使用移動頭:訪問移動端的站點,一般這種站點的反爬技術(shù)比較薄弱。
- 添加擴展:像正常使用瀏覽器一樣的功能。
- 設(shè)置編碼:應(yīng)對中文站,防止亂碼。
- 阻止JavaScript執(zhí)行
- ...
Chrome Options是一個配置chrome啟動時屬性的類,通過這個參數(shù)我們可以為Chrome添加如下參數(shù):
- 設(shè)置 chrome 二進制文件位置 (binary_location)
- 添加啟動參數(shù) (add_argument)
- 添加擴展應(yīng)用 (add_extension, add_encoded_extension)
- 添加實驗性質(zhì)的設(shè)置參數(shù) (add_experimental_option)
- 設(shè)置調(diào)試器地址 (debugger_address)
針對編碼格式的操作
# 設(shè)置默認編碼為 utf-8 from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('lang=zh_CN.UTF-8') driver = webdriver.Chrome(chrome_options = options)
針對UA請求頭的操作
# 設(shè)置請求頭為huaweiMeta10 Pro from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('User-Agent=Mozilla/5.0 (Linux; U; Android 8.1.0; zh-cn; BLA-AL00 Build/HUAWEIBLA-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/8.9 Mobile Safari/537.36') options.add_argument('--headless') # 瀏覽器不提供可視化頁面 driver = webdriver.Chrome(chrome_options = options)
針對禁止加載圖片的操作
# 設(shè)置瀏覽器禁止加載圖片 from selenium import webdriver options = webdriver.ChromeOptions() prefs = {"profile.managed_default_content_settings.images": 2} options.add_experimental_option("prefs", prefs) driver = webdriver.Chrome(chrome_options = options)
針對IP代理的操作
特別需要注意,在選擇代理時,盡量選擇靜態(tài)IP,才能提升爬取的穩(wěn)定性。如果使用動態(tài)匿名IP,每個IP的存活時間是很短的。
# 設(shè)置無賬號密碼的代理 chromeOptions = webdriver.ChromeOptions() chromeOptions.add_argument('--proxy-server=http://ip:port') driver = webdriver.Chrome(chrome_options=chromeOptions)
# 設(shè)置有賬號密碼的代理 proxyauth_plugin_path = create_proxyauth_extension( proxy_host='host', proxy_port='port', proxy_username="username", proxy_password="password" ) options.add_extension(proxyauth_plugin_path)
查看IP地址的鏈接:http://httpbin.org/ip
針對添加插件的操作
# 添加xpath helper應(yīng)用 from selenium import webdriver chrome_options = webdriver.ChromeOptions() # 設(shè)置好應(yīng)用擴展 extension_path = 'your file_path' chrome_options.add_extension(extension_path)
針對登錄時關(guān)閉彈出的密碼保存提示框
from selenium import webdriver from selenium.webdriver.common.by import By options = webdriver.ChromeOptions() prefs = {} # 設(shè)置這兩個參數(shù)就可以避免密碼提示框的彈出 prefs[“credentials_enable_service”] = False prefs[“profile.password_manager_enabled”] = False options.add_experimental_option(“prefs”, prefs) browser = webdriver.Chrome(chrome_options=options) browser.get('https://www.baidu.com/') browser.quit()
其它配置
options.add_argument('--disable-infobars') # 禁止策略化 options.add_argument('--no-sandbox') # 解決DevToolsActivePort文件不存在的報錯 options.add_argument('window-size=1920x3000') # 指定瀏覽器分辨率 options.add_argument('--disable-gpu') # 谷歌文檔提到需要加上這個屬性來規(guī)避bug options.add_argument('--incognito') # 隱身模式(無痕模式) options.add_argument('--disable-javascript') # 禁用javascript options.add_argument('--start-maximized') # 最大化運行(全屏窗口),不設(shè)置,取元素會報錯 options.add_argument('--disable-infobars') # 禁用瀏覽器正在被自動化程序控制的提示 options.add_argument('--hide-scrollbars') # 隱藏滾動條, 應(yīng)對一些特殊頁面 options.add_argument('blink-settings=imagesEnabled=false') # 不加載圖片, 提升速度 options.add_argument('--headless') # 瀏覽器不提供可視化頁面. linux下如果系統(tǒng)不支持可視化不加這條會啟動失敗 options.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" # 手動指定使用的瀏覽器位置
更多插件操作請參考:http://www.dbjr.com.cn/article/182967.htm
到此這篇關(guān)于python+selenium+Chrome options參數(shù)的使用的文章就介紹到這了,更多相關(guān)selenium Chrome options參數(shù) 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python爬取破解無線網(wǎng)絡(luò)wifi密碼過程解析
這篇文章主要介紹了Python爬取破解無線網(wǎng)絡(luò)密碼過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下2019-09-09Python數(shù)據(jù)分析庫pandas高級接口dt的使用詳解
這篇文章主要介紹了Python數(shù)據(jù)分析庫pandas高級接口dt的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12python編程簡單幾行代碼實現(xiàn)視頻轉(zhuǎn)換Gif示例
這篇文章主要為大家介紹了簡單使用幾行python代碼就可以實現(xiàn)將視頻轉(zhuǎn)換Gif的示例過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10