Tools to Work with the 'Splash' JavaScript Rendering Service in R
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3.5KB

---
output: rmarkdown::github_document
---

`splashr` : Tools to Work with the 'Splash' JavaScript Rendering Service

**Ridicuously basic functionality working at the moment. More coming soon**

TL;DR: This package works with Splash rendering servers which are really just a REST API & `lua` scripting interface to a QT browser. It's an alternative to the Selenium ecosystem and does not do everything Selenium can, but if you're just trying to get a page back that needs javascript rendering, this is a nice alternative.

You can also get it running with two commands:

sudo docker pull scrapinghub/splash
sudo docker run -p 5023:5023 -p 8050:8050 -p 8051:8051 scrapinghub/splash

(Do whatever you Windows ppl do with Docker on your systems to make ^^ work.)

All you need for this package to work is a running Splash instance. You provide the host/port for it and it's scrape-tastic from there.

### About Splash

>'Splash' <https://github.com/scrapinghub/splash> is a javascript rendering service. It’s a lightweight web browser with an 'HTTP' API, implemented in Python using 'Twisted'and 'QT' and provides some of the core functionality of the 'RSelenium' or 'seleniumPipes'R pacakges but with a Java-free footprint. The (twisted) 'QT' reactor is used to make the sever fully asynchronous allowing to take advantage of 'webkit' concurrency via QT main loop. Some of Splash features include the ability to process multiple webpages in parallel; retrieving HTML results and/or take screenshots; disabling images or use Adblock Plus rules to make rendering faster; executing custom JavaScript in page context; getting detailed rendering info in HAR format.

The following functions are implemented:

- `render_html`: Return the HTML of the javascript-rendered page.
- `render_jpeg`: Return a image (in JPEG format) of the javascript-rendered page.
- `render_png`: Return a image (in PNG format) of the javascript-rendered page.
- `splash`: Configure parameters for connecting to a Splash server
- `splashr`: Tools to Work with the 'Splash' JavaScript Rendering Service

### Installation

```{r eval=FALSE}
devtools::install_github("hrbrmstr/splashr")
```

```{r message=FALSE, warning=FALSE, error=FALSE}
options(width=120)
```

### Usage

```{r message=FALSE, warning=FALSE, error=FALSE}
library(splashr)
library(magick)
library(rvest)

# current verison
packageVersion("splashr")

splash("splash", 8050L) %>%
splash_active()

splash("splash", 8050L) %>%
splash_debug()
```

Notice the difference between a rendered HTML scrape and a non-rendered one:

```{r}
splash("splash", 8050L) %>%
render_html("http://marvel.com/universe/Captain_America_(Steve_Rogers)")

read_html("http://marvel.com/universe/Captain_America_(Steve_Rogers)")
```

Web page snapshots are easy-peasy too:

```{r eval=FALSE}
splash("splash", 8050L) %>%
render_png("http://marvel.com/universe/Captain_America_(Steve_Rogers)")
```

```{r eval=TRUE, include=FALSE}
splash("splash", 8050L) %>%
render_png("http://marvel.com/universe/Captain_America_(Steve_Rogers)") %>%
image_write("img/cap.png")
```

![](img/cap.png)

```{r eval=FALSE}
splash("splash", 8050L) %>%
render_jpeg("http://marvel.com/universe/Captain_America_(Steve_Rogers)")
```

```{r eval=TRUE, include=FALSE}
splash("splash", 8050L) %>%
render_jpeg("http://marvel.com/universe/Captain_America_(Steve_Rogers)") %>%
image_write("img/cap.jpg")
```

![](img/cap.jpg)

### Test Results

```{r message=FALSE, warning=FALSE, error=FALSE}
library(splashr)
library(testthat)

date()

test_dir("tests/")
```

```{r eval = FALSE, include = FALSE}