新selenium模拟登录知乎

本文介绍了一种通过设置Selenium的ChromeDriver为开发者模式的方法,以绕过知乎的反爬虫机制,避免因被检测到使用自动化脚本而产生的1001错误。通过调整Windows.navigator.webdriver属性,使网站无法识别出正在使用Selenium进行操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于参数请求错误1001

  • 由于知乎的反爬机制可以检测到selenium脚本运行被拒绝访问1001
  • 在chrome浏览器中的windows.navigator.webdriver结果为Ture,而正常使用浏览器的时候该值为False
  • 只需将设置其为开发者模式,防止被各大网站识别出来使用了Selenium
#设置为开发者模式
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
b=webdriver.Chrome(executable_path='C:\\Desktop\\chromedriver.exe',options=options)

步骤

  1. 获取链接
  2. 根据Xpath或者css获取位置
  3. 输入参数
  4. 点击登录

代码

from selenium import webdriver
from scrapy.selector import Selector

# 进入开发者模式
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
b=webdriver.Chrome(executable_path='C:\\Users\\23607\\Desktop\\py\\chromedriver.exe',options=options)

b.get('https://www.zhihu.com/signin?next=%2F')
#切换到密码登录
b.find_element_by_xpath('//*[@id="root"]/div/main/div/div/div[2]/div/form/div[1]/div[2]').click()

#根据css获取位置并输入账号密码
b.find_element_by_css_selector('.SignFlow-accountInputContainer div input[name="username"]').send_keys('账号')
b.find_element_by_css_selector('.SignFlow-password div div input[name="password"]').send_keys('密码')
#根据xpath获取相应位置并点击登录
b.find_element_by_xpath('//*[@id="root"]/div/main/div/div/div[2]/div/form/button').click()

 b.quit()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值