Ruby:Phantom.js在特定网站上被屏蔽了?

时间:2014-09-07 02:28:49

标签: ruby selenium capybara tumblr poltergeist

我正在使用capybara poltergeist在tumblr.com上自动化一个小脚本

我的脚本与我的Chrome驱动程序一起工作正常..我的恶作剧驱动程序加载所有其他网站就好了,但出于某种原因,当我尝试加载tumblr时会抛出Capybara::Poltergeist::StatusFailError

复制步骤:

$ brew install phantomjs
$ gem install capybara
$ gem install poltergeist
$ gem install selenium-webdriver
$ irb


require 'capybara/poltergeist'

module Drivers
  class Poltergeist < Capybara::Poltergeist::Driver
    def needs_server?
      false
    end
  end
end

Capybara.register_driver :poltergeist_errorless do |app|
  Drivers::Poltergeist.new(app, js_errors: false, timeout: 10000, phantomjs_options: ['--load-images=no', '--ignore-ssl-errors=yes'])
end

session = Capybara::Session.new(:poltergeist_errorless)
session.visit('https://google.com') # This works fine
session.visit('https://tumblr.com') # This does not work?

我尝试将所有标题设置为查看我的Google Chrome的请求,但这似乎也没有解决它。有没有人有任何建议?

1 个答案:

答案 0 :(得分:15)

问题与phantomjs SSL握手失败有关。您可以使用我的gist并使用phantomjs运行,您将看到:

[cut]
= onResourceError()
  - unable to load url: "https://www.tumblr.com/"
  - error code: 6, description: SSL handshake failed
= onResourceReceived()
  id: 3, stage: "end", response: {"contentType":null,"headers":[],"id":3,"redirectURL":null,"stage":"end","status":null,"statusText":null,"time":"2014-09-16T12:06:05.547Z","url":"https://www.tumblr.com/"}
= onLoadFinished()
  status: fail
DONE WITH  fail WebPage(name = "WebPage")

稍微检查一下解决方法是在幻像中使用--ssl-protocol=any,因此您的代码将变为:

Capybara.register_driver :poltergeist_errorless do |app|
  Drivers::Poltergeist.new(app, js_errors: false, timeout: 10000, phantomjs_options: ['--load-images=no', '--ignore-ssl-errors=yes', '--ssl-protocol=any'])
end

工作。

参考文献: