Skip to content

Instantly share code, notes, and snippets.

@belltailjp
Created March 5, 2016 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belltailjp/ad1648750aff09bf7979 to your computer and use it in GitHub Desktop.
Save belltailjp/ad1648750aff09bf7979 to your computer and use it in GitHub Desktop.
require 'capybara'
require 'capybara/poltergeist'
def wait_for_ajax(session)
Timeout.timeout(Capybara.default_wait_time) do
return if session.evaluate_script('jQuery.active').blank?
loop until session.evaluate_script('jQuery.active').zero?
end
end
def access(url, load_image: true, blacklist: [], whitelist: [])
Capybara.register_driver(:poltergeist) do |app|
Capybara::Poltergeist::Driver.new(app, {
js_errors: false,
phantomjs_options: ["--load-images=#{load_image ? 'yes' : 'no'}"]
})
end
s = Capybara::Session.new(:poltergeist)
s.driver.browser.url_blacklist = blacklist if blacklist.present?
s.driver.browser.url_whitelist = whitelist if whitelist.present?
s.visit(url)
Scraping.wait_for_ajax(s)
s
end
params = [true, false].product([[], %w[www.facebook.com]], [[], %w[www.jleague.jp]])
params.each do |load_image, blacklist, whitelist|
next if blacklist.present? and whitelist.present?
t = 10.times.map do
sleep(3)
Benchmark.realtime do
access('http://www.jleague.jp/match/j1/2016/022801/live/',
load_image: load_image,
blacklist: blacklist,
whitelist: whitelist)
end
end
puts "#{load_image.!}, #{blacklist.inspect}, #{whitelist.inspect}, #{(t.sum / t.length).round(2)}s"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment