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_。修改方法:
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開發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
總結
以上是生活随笔為你收集整理的selenium:反反爬拖动验证码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 炒橘核的功效与作用、禁忌和食用方法
- 下一篇: 拔阻生齿要多久好