How do I take screenshots of web pages using ruby

2019-01-31 15:09发布

I'm trying to programatically create thumbnail images of a large number of web pages that are hosted on my own ruby/rails-based website.

I want to be able to code a stand-alone bit of ruby that looks something like this:

require 'awesome-screenshot-maker'

items.each do |id|
  url = "http://foo.com/bar/#{id}"
  shooter = AwesomeScreenshotMaker.new(0.2) # thumbnails are 20% of original
  shooter.capture(url, "/images/thumbnail-#{id}.png")
end

I need the awesome-screenshot-maker library (and its dependencies) to be fairly easy to build on Linux, Solaris and Mac OS X. Ideally it will install with a single 'gem install' command.

I've spent the afternoon exploring various options, including Moz snap shooter, webkit2png and rbwebkitgtk. They are all in the right area, but none seem to work on all three platforms.

RMagick looks like a possible option if I'm willing to output PDFs from my rails app (instead of web pages), but that strikes me as hacky. It's also very laborious to get RMagic and imagemagick up and running on Mac OS X.

Does such a library exist that can easily be setup on three platforms?

5条回答
放荡不羁爱自由
2楼-- · 2019-01-31 15:35

There is a ruby wrapper for PageGlimpse available:

http://code.squidchunks.com/pageglimpse/

Unfortunately, the Terms of Use state that "You must not use or launch any automated system, including without limitation, "offline readers", "spiders," etc. to capture data provided by the Service."

查看更多
孤傲高冷的网名
3楼-- · 2019-01-31 15:43

You can use the gem gastly.

Gastly.capture('http://google.com', 'output.png')

查看更多
forever°为你锁心
4楼-- · 2019-01-31 15:44

There is https://github.com/maxwell/screencap which uses phantom.js

查看更多
爷、活的狠高调
5楼-- · 2019-01-31 15:48

Selenium RC has a Ruby interface and can grab a screenshot using capture_screenshot(filename,kwargs).

You'd then have to shrink it to a thumbnail.

查看更多
等我变得足够好
6楼-- · 2019-01-31 15:51

you might want to try this:

http://www.pageglimpse.com/

查看更多
登录 后发表回答