python selenium禁止加載某些請求的實現(xiàn)
問題描述
通過selenium請求目標網站時候, 真實數(shù)據(jù)(我這里是驗證碼圖片)已經加載出來, 由于網站做了第三方上報所以得等待很久, 但是上報這個請求不是必須的.
例如
驗證碼已經加載完成, 但是huatuo.qq.com響應時間過長 , webdriver.get()的機制是等待請求的url響應全部完成才進行下一步. 顯示等待和隱式等待的作用是每隔多少秒來檢測一下這個地址是否加載完成, 所以此處不生效.
那我要做的是: 當請求目標url時候, 希望webdriver不上報或者屏蔽huatuo.qq.com…這樣就能節(jié)省大量時間, 從而進行下一步操作

解決方案
在通過selenium打開目標url后, 植入js插件, 通過插件來屏蔽上報url
配置selenium屬性, 添加屏蔽規(guī)則
chrome_options.add_argument('--host-resolver-rules=MAP report.huatuo.qq.com 127.0.0.1')
最終效果

這樣就能專注于目標url, 更快的執(zhí)行下一步.
其他屬性配置
options.add_argument(‘headless') # 無頭模式
options.add_argument(‘window-size={}x{}'.format(width, height)) # 直接配置大小和set_window_size一樣
options.add_argument(‘disable-gpu') # 禁用GPU加速
options.add_argument(‘proxy-server={}'.format(self.proxy_server)) # 配置代理
options.add_argument('–no-sandbox') # 沙盒模式運行
options.add_argument('–disable-setuid-sandbox') # 禁用沙盒
options.add_argument('–disable-dev-shm-usage') # 大量渲染時候寫入/tmp而非/dev/shm
options.add_argument('–user-data-dir={profile_path}'.format(profile_path)) # 用戶數(shù)據(jù)存入指定文件
options.add_argument('no-default-browser-check) # 不做瀏覽器默認檢查
options.add_argument("–disable-popup-blocking") # 允許彈窗
options.add_argument("–disable-extensions") # 禁用擴展
options.add_argument("–ignore-certificate-errors") # 忽略不信任證書
options.add_argument("–no-first-run") # 初始化時為空白頁面
options.add_argument('–start-maximized') # 最大化啟動
options.add_argument('–disable-notifications') # 禁用通知警告
options.add_argument('–enable-automation') # 通知(通知用戶其瀏覽器正由自動化測試控制)
options.add_argument('–disable-xss-auditor') # 禁止xss防護
options.add_argument('–disable-web-security') # 關閉安全策略
options.add_argument('–allow-running-insecure-content') # 允許運行不安全的內容
options.add_argument('–disable-webgl') # 禁用webgl
options.add_argument('–homedir={}') # 指定主目錄存放位置
options.add_argument('–disk-cache-dir={臨時文件目錄}') # 指定臨時文件目錄
options.add_argument(‘disable-cache') # 禁用緩存
options.add_argument(‘excludeSwitches', [‘enable-automation']) # 開發(fā)者模式
參考
其他詳細配置 請點擊
到此這篇關于python selenium禁止加載某些請求的實現(xiàn)的文章就介紹到這了,更多相關python selenium禁止加載某些請求內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
用python打包exe應用程序及PyInstaller安裝方式
PyInstaller 制作出來的執(zhí)行文件并不是跨平臺的,如果需要為不同平臺打包,就要在相應平臺上運行PyInstaller進行打包。今天通過本文給大家介紹用python打包exe應用程序及PyInstaller安裝方式,感興趣的朋友一起看看吧2021-12-12
用Python的Django框架編寫從Google Adsense中獲得報表的應用
這篇文章主要介紹了用Python的Django框架編寫從Google Adsense中獲得報表的應用,主要利用了官方的Google Adsense API,需要的朋友可以參考下2015-04-04
windows下Anaconda的安裝與配置正解(Anaconda入門教程)
最近很多朋友學習python,很多朋友也推薦使用anaconda這個工具,但安裝以后也不會使用,這里腳本之家小編就為大家整理一下比較詳細的教程,方便自己也方便需要的朋友,希望大家以后多多支持腳本之家2018-04-04
Python3加密解密庫Crypto的RSA加解密和簽名/驗簽實現(xiàn)方法實例
這篇文章主要介紹了Python3加密解密庫Crypto的RSA加解密和簽名/驗簽實現(xiàn)方法實例,需要的朋友可以參考下2020-02-02
Python Diagrams庫以代碼形式生成云系統(tǒng)架構圖實例詳解
這篇文章主要介紹了Python Diagrams庫以代碼形式生成云系統(tǒng)架構圖實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01

