Python第三方庫undetected_chromedriver的使用
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)
#獲取內(nèi)容為亂碼可嘗試用以下方法解碼
#gzip.decompress(request.response.body).decode("utf-8")其中version_main可以根據(jù)瀏覽器版本指定版本號
注意:
使用seleniumwire.undetected_chromedriver有一個大坑
輸入executable_path不會生效,因為在webdriver的源碼是單獨引用的undetected_chromedriver

所以不會接收到傳入的executable_path。
而在undetected_chromedriver源碼中,如果沒有傳入path就會每次啟動去官網(wǎng)重新下載一個新的驅(qū)動器,再編譯成可執(zhí)行的文存放在以下目錄

解決辦法:
在webdriver的源碼中指定executable_path

這個帶有前綴id的chromedriver是有執(zhí)行權(quán)限的可執(zhí)行程序啦
(直接使用官網(wǎng)下載的可能會沒有權(quán)限,可以先直接運行一次,去到對應(yīng)目錄下面找到一個就可以永久使用啦<其他的可以刪除>)
總結(jié)
到此這篇關(guān)于Python第三方庫undetected_chromedriver使用的文章就介紹到這了,更多相關(guān)Python undetected_chromedriver使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對python同一個文件夾里面不同.py文件的交叉引用方法詳解
今天小編就為大家分享一篇對python同一個文件夾里面不同.py文件的交叉引用方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
使用Python和XML實現(xiàn)文件復(fù)制工具的完整代碼
在本篇博客中,我們將學(xué)習(xí)如何使用 wxPython 構(gòu)建一個簡單的文件復(fù)制工具,并將文件路徑和目標目錄的配置信息保存到 XML 文件中,通過這種方式,我們可以在下次運行程序時輕松加載之前保存的配置,需要的朋友可以參考下2024-08-08
python腳本監(jiān)控logstash進程并郵件告警實例
這篇文章主要介紹了python腳本監(jiān)控logstash進程并郵件告警實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python測試框架:pytest學(xué)習(xí)筆記
這篇文章主要介紹了Python測試框架:pytest的相關(guān)資料,幫助大家更好的利用python進行單元測試,感興趣的朋友可以了解下2020-10-10
從基礎(chǔ)到進階帶你玩轉(zhuǎn)Python中的JSON
JSON是一種輕量級的數(shù)據(jù)交換格式,在Python中處理JSON數(shù)據(jù)是日常開發(fā)中的常見任務(wù)之一,本文將詳細介紹如何在Python中處理JSON對象,需要的可以參考下2024-12-12
Python網(wǎng)絡(luò)爬蟲之獲取網(wǎng)絡(luò)數(shù)據(jù)
本文介紹了Python中用于獲取網(wǎng)絡(luò)數(shù)據(jù)的重要工具之一——Requests庫,詳細講解了Requests庫的基本使用方法、請求方法、請求頭、請求參數(shù)、Cookies、Session等內(nèi)容,并結(jié)合實例代碼展示了Requests庫的應(yīng)用場景2023-04-04

