美文网首页
selenium特征隐藏反检测反爬、webdriver每次打开页

selenium特征隐藏反检测反爬、webdriver每次打开页

作者: 生命不止运动不息 | 来源:发表于2024-01-17 22:48 被阅读0次

1、selenium特征隐藏反检测反爬

直接上答案:使用undetected-chromedriver版本,安装使用,自行搜索

示例:

import time
import undetected_chromedriver as uc
driver = uc.Chrome()
driver.get('https://bot.sannysoft.com/')
time.sleep(300)

2、undetected-chromedriver使用,报错Failed to establish a new connection: [Errno 61] Connection refused

原因:‘墙’ 的问题
解决:提前下载chromedriver,然后调用时主动指定(实例在后面)

chromedriver官网下载地址:
https://chromedriver.chromium.org/downloads/version-selection

github上的问题:
https://github.com/ultrafunkamsterdam/undetected-chromedriver/issues/1431

答案

3、webdriver每次打开页面都登录问题、chrome指定profile

之所以每次都需要登录,就是每次webdriver打开页面都是一个新的session。
因此就要使用同一个session的数据,所以就需要手动指定session数据的缓存profile的路径

这里要感谢以下文章:


每次登陆问题

这里不能贴太多链接:所以截图感谢!


指定profile

4、完整示例代码(Mac环境)

# 文章地址
# https://www.jianshu.com/p/8d8445ce59ac?v=1705589331227
# https://blog.csdn.net/weixin_42453905/article/details/122086184
# https://github.com/ultrafunkamsterdam/undetected-chromedriver
import time
import undetected_chromedriver as uc
from selenium.webdriver.chrome.options import Options

chrome_options = Options()

# chrome 默认的profile路径
profile = '/Users/wkun/Library/Application Support/Google/Chrome/Default'

# 将默认的profile路径中的内容,拷贝到以下目录中,并将该路径指派给chromedriver
profile = '/Users/wkun/Documents/Projects/Bill/GiteePython/miguautomedia/profile/Default'
chrome_options.add_argument(f"--user-data-dir={profile}")

# 手动下载好chromedriver,然后手动指定driver_executable_path,免得自动去下载,会被和谐
driverpath = '/Users/wkun/Documents/Projects/Bill/GiteePython/miguautomedia/profile/driver/chromedriver'
driver = uc.Chrome(options=chrome_options, driver_executable_path=driverpath)

driver.get('https://bot.sannysoft.com/')
time.sleep(300)

相关文章

网友评论

      本文标题:selenium特征隐藏反检测反爬、webdriver每次打开页

      本文链接:https://www.haomeiwen.com/subject/mddrodtx.html