Python第三方库undetected_chromedriver的使用

作者:Docda 时间:2022-12-06 14:29:27 

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

Python第三方库undetected_chromedriver的使用

所以不会接收到传入的executable_path。

而在undetected_chromedriver源码中,如果没有传入path就会每次启动去官网重新下载一个新的驱动器,再编译成可执行的文存放在以下目录

Python第三方库undetected_chromedriver的使用

解决办法:

      在webdriver的源码中指定executable_path

Python第三方库undetected_chromedriver的使用

这个带有前缀id的chromedriver是有执行权限的可执行程序啦

(直接使用官网下载的可能会没有权限,可以先直接运行一次,去到对应目录下面找到一个就可以永久使用啦<其他的可以删除>)

来源:https://blog.csdn.net/qq_43035475/article/details/125644970

标签:undetected,chromedriver,python,第三方库
0
投稿

猜你喜欢

  • python实现的人脸识别打卡系统

    2022-12-01 16:15:21
  • 仿google的asp分页代码

    2009-03-08 18:27:00
  • ORACLE 报警日志如何查看?第1/2页

    2009-07-02 12:06:00
  • Python使用微信itchat接口实现查看自己微信的信息功能详解

    2021-07-29 16:07:20
  • MySQL常用维护管理工具

    2009-03-08 16:51:00
  • 解决Django响应JsonResponse返回json格式数据报错问题

    2022-12-07 00:06:02
  • 聊聊Python中关于a=[[]]*3的反思

    2021-09-08 05:12:46
  • python得到电脑的开机时间方法

    2021-01-05 08:00:05
  • 使用xmlhttp为网站增加股市行情查询功能

    2007-10-10 21:09:00
  • python数据类型_字符串常用操作(详解)

    2023-10-12 05:02:57
  • Python3自动生成MySQL数据字典的markdown文本的实现

    2021-04-29 15:21:18
  • 关于Javascript的内存泄漏问题

    2008-04-15 07:46:00
  • asp彩色验证码的制作详解

    2007-09-18 13:22:00
  • Python字典循环添加一键多值的用法实例

    2021-07-15 14:48:48
  • 对Python3之方法的覆盖与super函数详解

    2023-08-25 19:05:28
  • php控制反转与依赖注入举例讲解

    2023-06-10 10:30:22
  • 地图网站的需求功能与体验

    2009-03-01 11:15:00
  • Python如何查看并打印matplotlib中所有的colormap(cmap)类型

    2023-12-05 11:04:28
  • XML简易教程之一

    2008-09-05 17:19:00
  • python网络爬虫之模拟登录 自动获取cookie值 验证码识别的具体实现

    2023-10-08 03:00:37
  • asp之家 网络编程 m.aspxhome.com