当selenium被识别爬虫后

因为某站发版,在修一个以前的项目,用Selenium驱动Chrome来做的,然后在某页面需要点击,无论怎么做都失效,我尝试过如下方法:

  1. 原始的点击,如:driver.find_element_by_id('id').click()
  2. 浏览器执行js,如:driver.execute_script('document.getElementById("id").click()')
  3. Selenium行为事件ActionChains,其中的move_to_element、move_to_element_with_offset等等方法都尝试过

最后,我手动在Selenium驱动打开的Chrome浏览器中去点击该按钮,但是无效。此刻我判断对方已经识别我的Chrome是爬虫了。

在stackoverflow上有一个问题,Can a website detect when you are using selenium with chromedriver?

下面有很多回答,其中有一个回答引用了某位CEO的讲话:

Even though they can create new bots, we figured out a way to identify Selenium the a tool they’re using, so we’re blocking Selenium no matter how many times they iterate on that bot. We’re doing that now with Python and a lot of different technologies. Once we see a pattern emerge from one type of bot, then we work to reverse engineer the technology they use and identify it as malicious.

所以Selenium并不是万能的,很多方法可以检查出你到底是不是爬虫,那么有什么应对方法呢?

有人回答说去修改 chromedriver 的源码,那还不如自己去写一个浏览器呢。

万万没想到,最后我还是成功了。

方法很简单,就是去驱动Firefox,而不是Chrome。

1self.driver = webdriver.Firefox()

就这样一行代码解决了。

至于里面的原因是什么,在网上找了很久Firefox与Chrome的区别,然后搜了下selenium的原理,如下

当Selenium2.x提出了WebDriver的概念之后,它提供了完全另外的一种方式与浏览器交互。那就是利用浏览器原生的API,封装成一套更加面向对象的SeleniumWebDriverAPI,直接操作浏览器页面里的元素,甚至操作浏览器本身(截屏,窗口大小,启动,关闭,安装插件,配置证书之类的)。由于使用的是浏览器原生的API,速度大大提高,而且调用的稳定性交给了浏览器厂商本身,显然是更加科学。然而带来的一些副作用就是,不同的浏览器厂商,对Web元素的操作和呈现多少会有一些差异,这就直接导致了SeleniumWebDriver要分浏览器厂商不同,而提供不同的实现。例如Firefox就有专门的FirefoxDriver,Chrome就有专门的ChromeDriver等等。

所以建议以后若发现驱动Chrome失败,可以尝试一下Firefox

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章