1. 程式人生 > 程式設計 >python+selenium+Chrome options引數的使用

python+selenium+Chrome options引數的使用

Chrome Options常用的行為一般有以下幾種:

  • 禁止圖片和視訊的載入:提升網頁載入速度。
  • 新增代理:用於翻牆訪問某些頁面,或者應對IP訪問頻率限制的反爬技術。
  • 使用移動頭:訪問移動端的站點,一般這種站點的反爬技術比較薄弱。
  • 新增擴充套件:像正常使用瀏覽器一樣的功能。
  • 設定編碼:應對中文站,防止亂碼。
  • 阻止JavaScript執行
  • ...

Chrome Options是一個配置chrome啟動時屬性的類,通過這個引數我們可以為Chrome新增如下引數:

  • 設定 chrome 二進位制檔案位置 (binary_location)
  • 新增啟動引數 (add_argument)
  • 新增擴充套件應用 (add_extension,add_encoded_extension)
  • 新增實驗性質的設定引數 (add_experimental_option)
  • 設定偵錯程式地址 (debugger_address)

針對編碼格式的操作

# 設定預設編碼為 utf-8
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('lang=zh_CN.UTF-8')
driver = webdriver.Chrome(chrome_options = options)

針對UA請求頭的操作

# 設定請求頭為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)

http://www.fynas.com/ua

針對禁止載入圖片的操作

# 設定瀏覽器禁止載入圖片
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代理的操作

特別需要注意,在選擇代理時,儘量選擇靜態IP,才能提升爬取的穩定性。如果使用動態匿名IP,每個IP的存活時間是很短的。

# 設定無賬號密碼的代理
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--proxy-server=http://ip:port') 
driver = webdriver.Chrome(chrome_options=chromeOptions)
# 設定有賬號密碼的代理
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應用

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()

# 設定好應用擴充套件
extension_path = 'your file_path'
chrome_options.add_extension(extension_path)

針對登入時關閉彈出的密碼儲存提示框

from selenium import webdriver 
from selenium.webdriver.common.by import By
options = webdriver.ChromeOptions() 
prefs = {} 
# 設定這兩個引數就可以避免密碼提示框的彈出
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') # 谷歌文件提到需要加上這個屬性來規避bug
options.add_argument('--incognito') # 隱身模式(無痕模式)
options.add_argument('--disable-javascript') # 禁用javascript
options.add_argument('--start-maximized') # 最大化執行(全屏視窗),不設定,取元素會報錯
options.add_argument('--disable-infobars') # 禁用瀏覽器正在被自動化程式控制的提示
options.add_argument('--hide-scrollbars') # 隱藏滾動條,應對一些特殊頁面
options.add_argument('blink-settings=imagesEnabled=false') # 不載入圖片,提升速度
options.add_argument('--headless') # 瀏覽器不提供視覺化頁面. linux下如果系統不支援視覺化不加這條會啟動失敗
options.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" # 手動指定使用的瀏覽器位置

更多外掛操作請參考:https://www.jb51.net/article/182967.htm

到此這篇關於python+selenium+Chrome options引數的使用的文章就介紹到這了,更多相關selenium Chrome options引數 內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!