日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python第三方庫undetected_chromedriver的使用_python

作者:Docda ? 更新時間: 2023-03-01 編程語言

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

欄目分類
最近更新