Amazon Linux 2015.09에 capybara-webkit gem을 설치하고 Ruby로 브라우징

Installing gems



capybara-webkit gem



젬 넣을 때까지 귀찮다. → AmazonLinux 2015.09에 Qt5 WebKit 설치
sudo ln -s /usr/lib64/qt5/bin/qmake /usr/bin/qmake 
sudo gem install capybara-webkit --no-ri --no-rdoc
Building native extensions.  This could take a while...
Successfully installed capybara-webkit-1.8.0
1 gem installed

headless gem


sudo yum install xorg-x11-server-Xvfb
sudo gem install headless --no-ri --no-rdoc
Fetching: headless-2.2.3.gem (100%)
Successfully installed headless-2.2.3
1 gem installed

루비로 HTML과 스크린 샷 얻기


require 'capybara-webkit'
require 'headless'

Capybara::Webkit.configure do |config|
  config.block_unknown_urls
  config.allow_url('*')
end

client = Capybara::Session.new(:webkit)
headless = Headless.new
headless.start
begin
  client.visit('http://www.google.com/')
  p client.status_code # => 200
  p client.html        # => "<!DOCTYPE html><html itemscope=\"\" itemtype=\"http://schema.org/WebPage\" ...
  client.save_screenshot('screenshot.png')
ensure
  headless.destroy
end

좋은 웹페이지 즐겨찾기