定制化开发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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

使用seleniumwire的undetected_chromedriver拓展,定制化开发好处是可以直接获取到浏览器的请求记录

from seleniumwire.undetected_chromedriver.v2 import Chrome, ChromeOptionsimport timeif __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")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

其中version_main可以根据浏览器版本指定版本号
注意:
      使用seleniumwire.undetected_chromedriver有一个大坑
      输入executable_path不会生效,因为在webdriver的源码是单独引用的undetected_chromedriver
所以不会接收到传入的executable_path。
而在undetected_chromedriver源码中,如果没有传入path就会每次启动去官网重新下载一个新的驱动器,再编译成可执行的文存放在以下目录
解决办法:
      在webdriver的源码中指定executable_path
这个带有前缀id的是有执行权限的可执行程序啦
(直接使用官网下载的可能会没有权限,可以先直接运行一次,去到对应目录下面找到一个就可以永久使用啦<其他的可以删除>)

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发