前言
- 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_。修改方法:
- 通过文本编辑器打开chromedriver。linux下,通过vim chromedriver打开。windows下,通过notepad++打开chromedriver.exe。
- 查找特征"cdc_",改为"chr_"。(删掉几个字符,则加几个字符。不能改变
var key = '$cdc_asdjflasutopfhvcZLmcfl_';
key字符串的长度。否则,会导致chromedriver程序无法执行) - 修改完成后保存。
Using Vim
vim /path/to/chromedriver
After running the line above, you’ll probably see a bunch of gibberish. Do the following:
- Search for
cdc_
by typing/cdc_
and pressing return.- Enable editing by pressing
a
.- Delete any amount of
$cdc_lasutopfhvcZLmcfl
and replace what was deleted with an equal amount characters. If you don’t,chromedriver
will fail.- After you’re done editing, press
esc
.
5.To save the changes and quit, type:wq!
and pressreturn
.- If you don’t want to save the changes, but you want to quit, type
:q!
and pressreturn
.- 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