selenium:反反爬拖动验证码

前言

  • springboot 2.0.0.RELEASE
  • selenium-java 3.9.1
  • chromedriver win32 2.33
  • chrome 62.0.3202.94
  • 你已经可以编写selenium程序爬一个没有验证码的网页,但现在碰到了一个拖动验证码:https://cf.aliyun.com/nocaptcha
    在这里插入图片描述在这里插入图片描述

总结

经过搜索、尝试、再搜索、再尝试…,终于发现需要使用两项技术对selenium进行反爬:

  • webdriver属性
  • 特征$cdc_asdjflasutopfhvcZLmcfl_

通过js脚本提取webdriver属性和特征$cdc_asdjflasutopfhvcZLmcfl_,将提取结果进行编码后发送给服务器。服务器发现是selenium爬虫后,拒绝验证。

知道了反爬,则反反爬也就好办了。逐项针对规避即可。
有大神已经将步骤整理:https://blog.csdn.net/sdzhr/article/details/86714328
下面解惑一下:

webdriver属性

Chrome从v63版本开始添加了webdriver属性。只要通过selenium调用了浏览器便会将这个属性设置为true。

当服务器通过js脚本识别到webdriver属性时,会有如下现象:

  • 手工启动chrome,人工拖动验证码可以通过验证
  • 使用selenium启动chrome,人工拖动验证码无法通过验证
  • 使用selenium启动chrome,程序模拟拖动验证码无法通过验证

所以,避开webdriver属性需要使用低于v63版本的Chrome(v62.0.3202.94版本测试通过)。

注:

  • 参考文章中提到的chrome 62.0.3202.62版本,未找到下载链接。
  • chrome和chromedriver之间是有对应关系的,版本不匹配则无法使用。chrome和chromedriver之间的对应关系
  • 换用Firefox,也会有webdriver属性。
  • 在浏览器的console控制台中输入navigator回车后,在输出中会发现webdriver属性,且值为true。

特征$cdc_asdjflasutopfhvcZLmcfl_

$cdc_asdjflasutopfhvcZLmcfl_是chromeDriver的特征之一。

chromeDriver会创建cdc_asdjflasutopfhvcZLmcfl_元素,并将该元素用于各种操作。比如,在使用findElement方法时,会使用该元素。

当服务器通过js脚本识别到特征“$cdc_asdjflasutopfhvcZLmcfl_”时,会有如下现象:

  • 手工启动chrome,人工拖动验证码可以通过验证
  • 使用selenium启动chrome,人工拖动验证码可以通过验证
  • 使用selenium启动chrome,程序模拟拖动验证码无法通过验证。(原因即为上面所述)

避开webdriver属性需要修改chromeDriver中的特征$cdc_asdjflasutopfhvcZLmcfl_。修改方法:

  1. 通过文本编辑器打开chromedriver。linux下,通过vim chromedriver打开。windows下,通过notepad++打开chromedriver.exe。
  2. 查找特征"cdc_",改为"chr_"。(删掉几个字符,则加几个字符。不能改变var key = '$cdc_asdjflasutopfhvcZLmcfl_';key字符串的长度。否则,会导致chromedriver程序无法执行)
  3. 修改完成后保存。

Using Vim

vim /path/to/chromedriver

After running the line above, you’ll probably see a bunch of gibberish. Do the following:

  1. Search for cdc_by typing /cdc_and pressing return.
  2. Enable editing by pressing a.
  3. Delete any amount of $cdc_lasutopfhvcZLmcfland replace what was deleted with an equal amount characters. If you don’t, chromedriverwill fail.
  4. After you’re done editing, press esc.
    5.To save the changes and quit, type :wq!and press return.
  5. If you don’t want to save the changes, but you want to quit, type :q!and press return.
  6. You’re done.

Go to the altered chromedriver and double click on it. A terminal window should open up. If you don’t see killed in the output, you successfully altered the driver.

springboot开发selenium程序

springboot开发selenium非常简单。可以参考:https://blog.csdn.net/l707268743/article/details/80942246

参考

https
?/blog.csdn.net/sdzhr/article/details/86714328
https://gitee.com/bobozhangyx/java-crawler/blob/master/file/编译后的chromedriver/
https://stackoverflow.com/questions/33225947/can-a-website-detect-when-you-are-using-selenium-with-chromedriver
https://blog.csdn.net/l707268743/article/details/80942246

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值