selenium:反反爬拖动验证码
前言
- springboot 2.0.0.RELEASE
- selenium-java 3.9.1
- chromedriver win32 2.33
- chrome 62.0.3202.94
- 你已經(jīng)可以編寫selenium程序爬一個(gè)沒有驗(yàn)證碼的網(wǎng)頁,但現(xiàn)在碰到了一個(gè)拖動(dòng)驗(yàn)證碼:https://cf.aliyun.com/nocaptcha
總結(jié)
經(jīng)過搜索、嘗試、再搜索、再嘗試…,終于發(fā)現(xiàn)需要使用兩項(xiàng)技術(shù)對(duì)selenium進(jìn)行反爬:
- webdriver屬性
- 特征$cdc_asdjflasutopfhvcZLmcfl_
通過js腳本提取webdriver屬性和特征$cdc_asdjflasutopfhvcZLmcfl_,將提取結(jié)果進(jìn)行編碼后發(fā)送給服務(wù)器。服務(wù)器發(fā)現(xiàn)是selenium爬蟲后,拒絕驗(yàn)證。
知道了反爬,則反反爬也就好辦了。逐項(xiàng)針對(duì)規(guī)避即可。
有大神已經(jīng)將步驟整理:https://blog.csdn.net/sdzhr/article/details/86714328
下面解惑一下:
webdriver屬性
Chrome從v63版本開始添加了webdriver屬性。只要通過selenium調(diào)用了瀏覽器便會(huì)將這個(gè)屬性設(shè)置為true。
當(dāng)服務(wù)器通過js腳本識(shí)別到webdriver屬性時(shí),會(huì)有如下現(xiàn)象:
- 手工啟動(dòng)chrome,人工拖動(dòng)驗(yàn)證碼可以通過驗(yàn)證。
- 使用selenium啟動(dòng)chrome,人工拖動(dòng)驗(yàn)證碼無法通過驗(yàn)證。
- 使用selenium啟動(dòng)chrome,程序模擬拖動(dòng)驗(yàn)證碼無法通過驗(yàn)證。
所以,避開webdriver屬性需要使用低于v63版本的Chrome(v62.0.3202.94版本測(cè)試通過)。
注:
- 參考文章中提到的chrome 62.0.3202.62版本,未找到下載鏈接。
- chrome和chromedriver之間是有對(duì)應(yīng)關(guān)系的,版本不匹配則無法使用。chrome和chromedriver之間的對(duì)應(yīng)關(guān)系。
- 換用Firefox,也會(huì)有webdriver屬性。
- 在瀏覽器的console控制臺(tái)中輸入navigator回車后,在輸出中會(huì)發(fā)現(xiàn)webdriver屬性,且值為true。
特征$cdc_asdjflasutopfhvcZLmcfl_
$cdc_asdjflasutopfhvcZLmcfl_是chromeDriver的特征之一。
chromeDriver會(huì)創(chuàng)建cdc_asdjflasutopfhvcZLmcfl_元素,并將該元素用于各種操作。比如,在使用findElement方法時(shí),會(huì)使用該元素。
當(dāng)服務(wù)器通過js腳本識(shí)別到特征“$cdc_asdjflasutopfhvcZLmcfl_”時(shí),會(huì)有如下現(xiàn)象:
- 手工啟動(dòng)chrome,人工拖動(dòng)驗(yàn)證碼可以通過驗(yàn)證。
- 使用selenium啟動(dòng)chrome,人工拖動(dòng)驗(yàn)證碼可以通過驗(yàn)證。
- 使用selenium啟動(dòng)chrome,程序模擬拖動(dòng)驗(yàn)證碼無法通過驗(yàn)證。(原因即為上面所述)
避開webdriver屬性需要修改chromeDriver中的特征$cdc_asdjflasutopfhvcZLmcfl_。修改方法:
Using Vim
vim /path/to/chromedriverAfter running the line above, you’ll probably see a bunch of gibberish. Do the following:
5.To save the changes and quit, type :wq!and press return.
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開發(fā)selenium程序
springboot開發(fā)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
總結(jié)
以上是生活随笔為你收集整理的selenium:反反爬拖动验证码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 炒橘核的功效与作用、禁忌和食用方法
- 下一篇: git:config命令