網站首頁 編程語言 正文
undetected_chromedriver是專門針對瀏覽器識別做出來的拓展
直接使用undetected_chromedriver第三方庫
if __name__ == '__main__':
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
import undetected_chromedriver.v2 as uc
chrome_options = uc.ChromeOptions()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument("--profile-directory=Default")
chrome_options.add_argument("--ignore-certificate-errors")
chrome_options.add_argument("--disable-plugins-discovery")
chrome_options.add_argument("--incognito")
chrome_options.add_argument('--no-first-run')
chrome_options.add_argument('--no-service-autorun')
chrome_options.add_argument('--no-default-browser-check')
chrome_options.add_argument('--password-store=basic')
chrome_options.add_argument('--no-sandbox')
driver = uc.Chrome(options=chrome_options, executable_path='./driver/chromedriver')
driver.delete_all_cookies()
driver.get("https://accounts.google.com/signin/v2/identifier?service=accountsettings&continue=https%3A%2F%2Fmyaccount.google.com%3Futm_source%3Daccount-marketing-page%26utm_medium%3Dgo-to-account-button&flowName=GlifWebSignIn&flowEntry=ServiceLogin")
driver.find_element_by_xpath('//input[@type="email"]').send_keys(email)
input = WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.XPATH, '//*[@id="identifierNext"]')))
input.click()
WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input')))
driver.find_element_by_xpath('//*[@id="password"]/div[1]/div/div[1]/input').send_keys(password)
input = WebDriverWait(driver, 100).until(expected_conditions.element_to_be_clickable((By.XPATH, '//*[@id="passwordNext"]/div/button')))
input.click()
time.sleep(5)
cookies = driver.get_cookies()
cookies_arr = []
for c in cookies:
if c['domain'].endswith('.google.com'):
cookies_arr.append(f'{c["name"]}={c["value"]}')
driver.close()
return "; ".join(cookies_arr)
使用seleniumwire的undetected_chromedriver拓展,好處是可以直接獲取到瀏覽器的請求記錄
from seleniumwire.undetected_chromedriver.v2 import Chrome, ChromeOptions
import time
if __name__ == '__main__':
options = {}
chrome_options = ChromeOptions()
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--disable-dev-shm-usage")
# chrome_options.add_argument("--headless")
chrome_options.add_argument(f"--proxy-server=http://192.168.100.24:60021")
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument("--profile-directory=Default")
chrome_options.add_argument("--ignore-certificate-errors")
chrome_options.add_argument("--disable-plugins-discovery")
chrome_options.add_argument('--no-first-run')
chrome_options.add_argument('--no-service-autorun')
chrome_options.add_argument('--no-default-browser-check')
chrome_options.add_argument('--password-store=basic')
chrome_options.add_argument('--no-sandbox')
browser = Chrome(seleniumwire_options=options, options=chrome_options,executable_path='C:\Program Files\Google\Chrome\Application\chromedriver.exe',version_main=101)
browser.get('https://portal.thecourierguy.co.za/track?ref=TCG107468416T')
time.sleep(15)
print(browser.page_source)
for request in browser.requests:
if request.response:
print(request.path)
if 'shipments' in request.path:
print(request.response.body)
#獲取內容為亂碼可嘗試用以下方法解碼
#gzip.decompress(request.response.body).decode("utf-8")
其中version_main可以根據瀏覽器版本指定版本號
注意:
? ? ? 使用seleniumwire.undetected_chromedriver有一個大坑
? ? ? 輸入executable_path不會生效,因為在webdriver的源碼是單獨引用的undetected_chromedriver
所以不會接收到傳入的executable_path。
而在undetected_chromedriver源碼中,如果沒有傳入path就會每次啟動去官網重新下載一個新的驅動器,再編譯成可執行的文存放在以下目錄
解決辦法:
? ? ? 在webdriver的源碼中指定executable_path
這個帶有前綴id的chromedriver是有執行權限的可執行程序啦
(直接使用官網下載的可能會沒有權限,可以先直接運行一次,去到對應目錄下面找到一個就可以永久使用啦<其他的可以刪除>)
總結
原文鏈接:https://blog.csdn.net/qq_43035475/article/details/125644970
相關推薦
- 2022-03-16 詳解C語言在STM32中的內存分配問題_C 語言
- 2024-03-14 docker創建自定義網絡
- 2022-08-03 使用Go構建一款靜態分析工具Owl詳解_Golang
- 2022-06-11 Python?tkinter庫圖形繪制例子分享_python
- 2023-07-22 JVM的類加載機制和垃圾回收機制
- 2022-08-12 C#?Unicode編碼解碼的實現_C#教程
- 2022-08-20 C++?基礎函數的介紹及使用(Vector+deque+STL)_C 語言
- 2022-07-07 python?函數定位參數+關鍵字參數+inspect模塊_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支