Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I screenshot a website using R?

Tags:

r

rcurl

httr

So I'm not 100% sure this is possible, but I found a good solution in Ruby and in python, so I was wondering if something similar might work in R.

Basically, given a URL, I want to render that URL, take a screenshot of the rendering as a .png, and save the screenshot to a specified folder. I'd like to do all of this on a headless linux server.

Is my best solution here going to be running system calls to a tool like CutyCapt, or does there exist an R-based toolset that will help me solve this problem?

like image 976
Zach Avatar asked Apr 02 '15 17:04

Zach


People also ask

How do I take a screenshot of a portfolio website?

On Windows, the key combination Alt + PrtSc (Print Screen) captures the currently active window. Like on Mac, the screenshot will be copied into the clipboard, so you can insert it into other software by pressing Ctrl + V .


2 Answers

You can take screenshots using Selenium:

library(RSelenium)
rD <- rsDriver(browser = "phantomjs")
remDr <- rD[['client']]
remDr$navigate("http://www.r-project.org")
remDr$screenshot(file = tf <- tempfile(fileext = ".png"))
shell.exec(tf) # on windows
remDr$close()
rD$server$stop()

In earlier versions, you were able to do:

library(RSelenium)
startServer()
remDr <- remoteDriver$new()
remDr$open()
remDr$navigate("http://www.r-project.org")
remDr$screenshot(file = tf <- tempfile(fileext = ".png"))
shell.exec(tf) # on windows
like image 63
lukeA Avatar answered Sep 19 '22 19:09

lukeA


I haven't tested it, but this open source project seems to do exactly that: https://github.com/wch/webshot

It is a easy as:

library(webshot)
webshot("https://www.r-project.org/", "r.png")
like image 25
nassimhddd Avatar answered Sep 19 '22 19:09

nassimhddd