Python第三方庫(kù)undetected_chromedriver的使用
undetected_chromedriver是專門針對(duì)瀏覽器識(shí)別做出來(lái)的拓展
直接使用undetected_chromedriver第三方庫(kù)
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拓展,好處是可以直接獲取到瀏覽器的請(qǐng)求記錄
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ù)瀏覽器版本指定版本號(hào)
注意:
使用seleniumwire.undetected_chromedriver有一個(gè)大坑
輸入executable_path不會(huì)生效,因?yàn)樵趙ebdriver的源碼是單獨(dú)引用的undetected_chromedriver
所以不會(huì)接收到傳入的executable_path。
而在undetected_chromedriver源碼中,如果沒(méi)有傳入path就會(huì)每次啟動(dòng)去官網(wǎng)重新下載一個(gè)新的驅(qū)動(dòng)器,再編譯成可執(zhí)行的文存放在以下目錄
解決辦法:
在webdriver的源碼中指定executable_path
這個(gè)帶有前綴id的chromedriver是有執(zhí)行權(quán)限的可執(zhí)行程序啦
(直接使用官網(wǎng)下載的可能會(huì)沒(méi)有權(quán)限,可以先直接運(yùn)行一次,去到對(duì)應(yīng)目錄下面找到一個(gè)就可以永久使用啦<其他的可以刪除>)
總結(jié)
到此這篇關(guān)于Python第三方庫(kù)undetected_chromedriver使用的文章就介紹到這了,更多相關(guān)Python undetected_chromedriver使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于pip install uwsgi安裝失敗問(wèn)題的解決方案
這篇文章主要介紹了關(guān)于pip install uwsgi安裝失敗問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Jupyter Notebook如何導(dǎo)入python文件時(shí)的問(wèn)題
這篇文章主要介紹了Jupyter Notebook如何導(dǎo)入python文件時(shí)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Python使用指定端口進(jìn)行http請(qǐng)求的例子
今天小編就為大家分享一篇Python使用指定端口進(jìn)行http請(qǐng)求的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07用Python監(jiān)控NASA TV直播畫面的實(shí)現(xiàn)步驟
本文分享一個(gè)名為"Spacestills"的開源程序,它可以用于查看 NASA TV 的直播畫面(靜止幀)2021-05-05python中正則表達(dá)式findall的用法實(shí)例
在寫著自動(dòng)化測(cè)試的腳本時(shí)重新復(fù)習(xí)了一下正則表達(dá)式findall()方法,下面這篇文章主要給大家介紹了關(guān)于python中正則表達(dá)式findall用法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09tf.truncated_normal與tf.random_normal的詳細(xì)用法
本篇文章主要介紹了tf.truncated_normal與tf.random_normal的詳細(xì)用法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03通過(guò)OpenCV實(shí)現(xiàn)對(duì)指定顏色的物體追蹤
這篇文章主要介紹的是通過(guò)OpenCV實(shí)現(xiàn)對(duì)特定顏色的物體追蹤,文中實(shí)驗(yàn)用的是綠蘿的樹葉。本文的示例代碼講解詳細(xì),對(duì)學(xué)習(xí)OPenCV有一定的幫助,感興趣的小伙伴可以了解一下2021-12-12