diff --git a/DESCRIPTION b/DESCRIPTION index acf2465..86b1c87 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: splashr Type: Package Title: Tools to Work with the 'Splash' 'JavaScript' Rendering and Scraping Service -Version: 0.6.0 -Date: 2019-02-24 +Version: 0.7.0 +Date: 2020-02-18 Encoding: UTF-8 Authors@R: c( person("Bob", "Rudis", email = "bob@rud.is", role = c("aut", "cre"), @@ -46,5 +46,5 @@ Imports: HARtools, jsonlite, lubridate -RoxygenNote: 6.1.1 +RoxygenNote: 7.0.2 VignetteBuilder: knitr diff --git a/NAMESPACE b/NAMESPACE index fddc8a8..7259b47 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,17 +1,11 @@ # Generated by roxygen2: do not edit by hand -S3method(as.data.frame,har) -S3method(as.data.frame,harentries) -S3method(as.data.frame,harentry) S3method(print,splash_debug) S3method(print,splash_json) S3method(print,splashr) export("%>%") export(HARviewer) export(HARviewerOutput) -export(as_data_frame.har) -export(as_data_frame.harentries) -export(as_data_frame.harentry) export(as_har) export(as_httr_req) export(as_response) @@ -81,6 +75,7 @@ export(splash_version) export(splash_wait) export(start_splash) export(stop_splash) +export(tidy_har) export(ua_android_samsung) export(ua_apple_tv) export(ua_chromecast) diff --git a/NEWS.md b/NEWS.md index 905ceb7..0fcd70b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +0.7.0 +* Changed `as[._]data[._]frame()` function to `tidy_har()` +* Improved API error messages +* Added support for `http2` () +* Added support for the alpha/experimental Splash chromium engine + () + 0.6.0 * Switch Docker orchestration to the `stevedore` package diff --git a/R/as-data-frame-har.R b/R/as-data-frame-har.R index 6f70ba2..d0c9c34 100644 --- a/R/as-data-frame-har.R +++ b/R/as-data-frame-har.R @@ -1,62 +1,116 @@ -#' Turns a "HAR"-like object into a data frame(tibble) -#' -#' @md -#' @param x A `harentry` object -#' @param ... ignored -#' @return data frame (tibble) -#' @export -as_data_frame.harentry <- function(x, ...) { - - req <- x$request - resp <- x$response - - data_frame( - request_url = req$url, - request_method = req$method, - request_http_version = req$httpVersion, - request_query_string = list(req$queryString), - request_header_size = req$headersSize, - requestheaders = list(if (length(req$headers) > 0) flatten_df(req$headers) else data_frame()), - requestcookies = list(if (length(req$cookies) > 0) flatten_df(req$cookies) else data_frame()), - response_url = resp$url, - response_http_version = resp$httpVersion, - status_text = resp$statusText, - status = resp$status, - ok = resp$ok, - redirect_url = resp$redirectURL, - response_headers_size = resp$headersSize, - response_headers = list(if (length(resp$headers) > 0) flatten_df(resp$headers) else data_frame()), - response_cookies = list(if (length(resp$cookies) > 0) flatten_df(resp$cookies) else data_frame()), - response_body_size = resp$bodySize, - content_type = resp$content$mimeType, - content_encoding = resp$content$encoding %||% NA_character_, - content_size = resp$content$size, - content = resp$content$text %||% NA_character_ - ) +.tidy_one_entry <- function(x, include_content = TRUE) { -} + .x <- x -#' @rdname as_data_frame.harentry -#' @export -as_data_frame.harentries <- function(x, ...) { - map_df(x, as_data_frame) -} + if (length(.x[["timings"]])) { + data.frame( + stage = names(.x[["timings"]]), + value = unlist(.x[["timings"]], use.names = FALSE), + stringsAsFactors = FALSE + ) -> timings + } else { + timings <- data.frame(stringsAsFactors = FALSE) + } + class(timings) <- c("tbl_df", "tbl", "data.frame") + + + if (length(.x[["request"]][["headers"]])) { + data.frame( + name =vapply(.x[["request"]][["headers"]], `[[`, character(1), "name", USE.NAMES = FALSE), + value = vapply(.x[["request"]][["headers"]], `[[`, character(1), "value", USE.NAMES = FALSE), + stringsAsFactors = FALSE + ) -> req_headers + } else { + req_headers <- data.frame(stringsAsFactors = FALSE) + } + class(req_headers) <- c("tbl_df", "tbl", "data.frame") + + if (length(.x[["response"]][["headers"]])) { + data.frame( + name =vapply(.x[["response"]][["headers"]], `[[`, character(1), "name", USE.NAMES = FALSE), + value = vapply(.x[["response"]][["headers"]], `[[`, character(1), "value", USE.NAMES = FALSE), + stringsAsFactors = FALSE + ) -> headers + } else { + headers <- data.frame(stringsAsFactors = FALSE) + } + class(headers) <- c("tbl_df", "tbl", "data.frame") + + if (length(.x[["request"]][["cookies"]])) { + data.frame( + name =vapply(.x[["request"]][["cookies"]], `[[`, character(1), "name", USE.NAMES = FALSE), + value = vapply(.x[["request"]][["cookies"]], `[[`, character(1), "value", USE.NAMES = FALSE), + stringsAsFactors = FALSE + ) -> req_cookies + } else { + req_cookies <- data.frame(stringsAsFactors = FALSE) + } + class(req_cookies) <- c("tbl_df", "tbl", "data.frame") + + if (length(.x[["response"]][["cookies"]])) { + data.frame( + name =vapply(.x[["response"]][["cookies"]], `[[`, character(1), "name", USE.NAMES = FALSE), + value = vapply(.x[["response"]][["cookies"]], `[[`, character(1), "value", USE.NAMES = FALSE), + stringsAsFactors = FALSE + ) -> res_cookies + } else { + res_cookies <- data.frame(stringsAsFactors = FALSE) + } + class(res_cookies) <- c("tbl_df", "tbl", "data.frame") + + data.frame( + status = .x[["response"]][["status"]] %l0% NA_character_, + started = .x[["startedDateTime"]] %l0% NA_character_, + total_time = .x[["time"]] %l0% NA_integer_, + page_ref = .x[["pageref"]] %l0% NA_character_, + timings = I(list(timings)), + req_url = .x[["request"]][["url"]] %l0% NA_character_, + req_method = .x[["request"]][["method"]] %l0% NA_character_, + req_http_version = .x[["request"]][["httpVersion"]] %l0% NA_character_, + req_hdr_size = .x[["request"]][["headersSize"]] %l0% NA_character_, + req_headers = I(list(req_headers)), + req_cookies = I(list(req_cookies)), + resp_url = .x[["response"]][["url"]] %l0% NA_character_, + resp_rdrurl = .x[["response"]][["redirectURL"]] %l0% NA_character_, + resp_type = .x[["response"]][["content"]][["mimeType"]] %l0% NA_character_, + resp_size = .x[["resonse"]][["bodySize"]] %l0% NA_integer_, + resp_cookies = I(list(res_cookies)), + resp_headers = I(list(headers)), + resp_encoding = .x[["resonse"]][["content"]][["encoding"]] %l0% NA_character_, + resp_content_size = as.numeric(.x[["resonse"]][["content"]][["size"]]) %l0% NA_real_, + stringsAsFactors = FALSE + ) -> out + + if (include_content) out$resp_content <- .x[["resonse"]][["content"]][["text"]] %l0% NA_character_ + + class(out) <- c("tbl_df", "tbl", "data.frame") + + out -#' @rdname as_data_frame.harentry -#' @export -as_data_frame.har <- function(x, ...) { - as_data_frame(x$log$entries) } +#' Turn a gnHARly HAR object into a tidy data frame (tibble) +#' +#' @md +#' @param x A `harentry` object +#' @param include_content if `TRUE` (the default) the encoded element content will be returned in the data frame +#' @return data frame (tibble) #' @export -#' @rdname as_data_frame.harentry -as.data.frame.har <- as_data_frame.har +tidy_har <- function(.x, include_content = TRUE) { -#' @export -#' @rdname as_data_frame.harentry -as.data.frame.harentries <- as_data_frame.harentries + if (inherits(.x, "har")) { + out <- tidy_har(.x[["log"]][["entries"]], include_content = include_content) + } else if (inherits(.x, "harlog")) { + out <- tidy_har(.x[["entries"]], include_content = include_content) + } else if (inherits(.x, "harentries")) { + out <- do.call(rbind.data.frame, lapply(.x, .tidy_one_entry, include_content = include_content)) + class(out) <- c("tbl_df", "tbl", "data.frame") + } else if (inherits(.x, "harentry")) { + out <- .tidy_one_entry(.x, include_content = include_content) + } else { + stopifnot(inherits(.x, "harentries")) + } -#' @export -#' @rdname as_data_frame.harentry -as.data.frame.harentry <- as_data_frame.harentry + out +} diff --git a/R/check-or-report-status.R b/R/check-or-report-status.R new file mode 100644 index 0000000..00d3591 --- /dev/null +++ b/R/check-or-report-status.R @@ -0,0 +1,15 @@ +check_or_report_status <- function(res) { + + if (httr::status_code(res) >= 400) { + msg <- httr::content(res, as="text", encoding="UTF-8") + msg <- jsonlite::fromJSON(msg) + httr::stop_for_status( + x = res, + task = sprintf( + fmt = "render due to %s", + paste0(c(msg[["type"]], msg[["description"]], msg[["info"]]), collapse="; ") + ) + ) + } + +} diff --git a/R/render-har.r b/R/render-har.r index 34fa1d9..98d03c1 100644 --- a/R/render-har.r +++ b/R/render-har.r @@ -13,12 +13,24 @@ render_har <- function(splash_obj = splash_local, url, base_url, response_body=FALSE, timeout=30, resource_timeout, wait=0, proxy, js, js_src, filters, allowed_domains, allowed_content_types, forbidden_content_types, viewport="1024x768", images, headers, body, - http_method, save_args, load_args) { + http_method, save_args, load_args, http2 = FALSE, + engine = c("webkit", "chromium")) { wait <- check_wait(wait) - params <- list(url=url, timeout=timeout, wait=wait, viewport=jsonlite::unbox(viewport), - response_body=as.numeric(response_body)) + engine <- match.arg(engine[1], c("webkit", "chromium")) + + http2 <- ifelse(engine == "chromium", 1, as.integer(as.logical(http2[1]))) + + list( + url = url, + timeout = timeout, + wait = wait, + viewport = jsonlite::unbox(viewport), + response_body = as.numeric(response_body), + http2 = http2, + engine = engine + ) -> params if (!missing(base_url)) params$base_url <- jsonlite::unbox(base_url) if (!missing(resource_timeout)) params$resource_timeout <- resource_timeout @@ -45,7 +57,7 @@ render_har <- function(splash_obj = splash_local, url, base_url, response_body=F ) } - httr::stop_for_status(res) + check_or_report_status(res) out <- httr::content(res, as="text", encoding="UTF-8") spl <- jsonlite::fromJSON(out, flatten=FALSE, simplifyVector=FALSE) diff --git a/R/render-html.r b/R/render-html.r index 222d82b..a607a86 100644 --- a/R/render-html.r +++ b/R/render-html.r @@ -5,9 +5,9 @@ #' @md #' @param splash_obj Object created by a call to [splash()] #' @param url The URL to render (required) -#' @param base_url The base url to render the page with. +#' @param base_url The base URL to render the page with. #' @param timeout A timeout (in seconds) for the render (defaults to 30). Without -#' reconfiguring the startup parameters of the Splash server (not this package) +#' re-configuring the start-up parameters of the Splash server (not this package) #' the maximum allowed value for the timeout is 60 seconds. #' @param resource_timeout A timeout (in seconds) for individual network requests. #' @param wait Time (in seconds) to wait for updates after page is loaded (defaults to 0). @@ -25,13 +25,16 @@ #' present, Splash will abort any request if the response’s content type matches #' any of the content types in this list. Wildcards are supported. #' @param viewport View width and height (in pixels) of the browser viewport to render the -#' web page. Format is “x”, e.g. 800x600. Default value is "full". +#' web page. Format is “width>xheight”, e.g. 800x600. Default value is "full". #' @param images Whether to download images. #' @param headers HTTP headers to set for the first outgoing request. #' @param body Body of HTTP POST request to be sent if method is POST. #' @param http_method HTTP method of outgoing Splash request. #' @param save_args A list of argument names to put in cache. #' @param load_args Parameter values to load from cache +#' @param http2 Enable or disable HTTP2 support. `TRUE` to enable; `FALSE` to disable; defaults to `FALSE` +#' when `engine` is `webkit` due to malformed behaviour in 3.4.x of Splash +#' @param engine one of `webkit` or `chromium`; defaults to `webkit` #' @param raw_html if `TRUE` then return a character vector vs an XML document. Only valid for `render_html` #' @family splash_renderers #' @return An XML document. Note that this is processed by [xml2::read_html()] so it will not be @@ -43,11 +46,23 @@ render_html <- function(splash_obj = splash_local, url, base_url, timeout=30, resource_timeout, wait=0, proxy, js, js_src, filters, allowed_domains, allowed_content_types, forbidden_content_types, viewport="1024x768", images, headers, body, - http_method, save_args, load_args, raw_html=FALSE) { + http_method, save_args, load_args, http2 = FALSE, + engine = c("webkit", "chromium"), raw_html=FALSE) { wait <- check_wait(wait) - params <- list(url=url, timeout=timeout, wait=wait, viewport=jsonlite::unbox(viewport)) + engine <- match.arg(engine[1], c("webkit", "chromium")) + + http2 <- ifelse(engine == "chromium", 1, as.integer(as.logical(http2[1]))) + + list( + url = url, + timeout = timeout, + wait = wait, + viewport = jsonlite::unbox(viewport), + http2 = http2, + engine = engine + ) -> params if (!missing(base_url)) params$base_url <- jsonlite::unbox(base_url) if (!missing(resource_timeout)) params$resource_timeout <- resource_timeout @@ -74,7 +89,7 @@ render_html <- function(splash_obj = splash_local, url, base_url, timeout=30, re ) } - httr::stop_for_status(res) + check_or_report_status(res) out <- httr::content(res, as="text", encoding="UTF-8") @@ -82,4 +97,4 @@ render_html <- function(splash_obj = splash_local, url, base_url, timeout=30, re out -} \ No newline at end of file +} diff --git a/R/render-jpg.r b/R/render-jpg.r index 88efc15..1dc9c83 100644 --- a/R/render-jpg.r +++ b/R/render-jpg.r @@ -13,15 +13,29 @@ render_jpeg <- render_jpg <- function( timeout=30, resource_timeout, wait=0, render_all=TRUE, proxy, js, js_src, filters, allowed_domains, allowed_content_types, forbidden_content_types, viewport="full", images, headers, body, - http_method, save_args, load_args) { + http_method, save_args, load_args, http2 = FALSE, + engine = c("webkit", "chromium")) { wait <- check_wait(wait) - params <- list(url=url, timeout=timeout, - wait=if (render_all & wait == 0) 0.5 else wait, - viewport=viewport, - quality=quality, - render_all=as.numeric(render_all)) + engine <- match.arg(engine[1], c("webkit", "chromium")) + + http2 <- if (engine == "chromium") NULL else as.integer(as.logical(http2[1])) + viewport <- if (engine == "chromium") NULL else jsonlite::unbox(viewport[1]) + render_all <- if (engine == "chromium") NULL else as.integer(render_all[1]) + + wait <- if (is.null(render_all)) 0.5 else if (render_all & wait == 0) 0.5 else wait + + list( + url = url, + timeout = timeout, + wait = wait, + viewport = viewport, + render_all = render_all, + quality = quality, + http2 = http2, + engine = engine + ) -> params if (!missing(width)) params$width <- width if (!missing(height)) params$height <- height @@ -51,7 +65,7 @@ render_jpeg <- render_jpg <- function( ) } - httr::stop_for_status(res) + check_or_report_status(res) magick::image_read(httr::content(res, as="raw")) diff --git a/R/render-png.r b/R/render-png.r index ec54f24..57b251d 100644 --- a/R/render-png.r +++ b/R/render-png.r @@ -18,15 +18,27 @@ render_png <- function( timeout=30, resource_timeout, wait=0, render_all=TRUE, proxy, js, js_src, filters, allowed_domains, allowed_content_types, forbidden_content_types, viewport="full", images, headers, body, - http_method, save_args, load_args) { + http_method, save_args, load_args, http2 = FALSE, + engine = c("webkit", "chromium")) { + wait <- check_wait(wait) - params <- list( + engine <- match.arg(engine[1], c("webkit", "chromium")) + + http2 <- if (engine == "chromium") NULL else as.integer(as.logical(http2[1])) + viewport <- if (engine == "chromium") NULL else jsonlite::unbox(viewport[1]) + render_all <- if (engine == "chromium") NULL else as.integer(render_all[1]) + + wait <- if (is.null(render_all)) 0.5 else if (render_all & wait == 0) 0.5 else wait + + list( url = url, timeout = timeout, - wait = if (render_all & wait == 0) 0.5 else wait, - viewport = jsonlite::unbox(viewport), - render_all = as.numeric(render_all) - ) + wait = wait, + viewport = viewport, + render_all = render_all, + http2 = http2, + engine = engine + ) -> params if (!missing(width)) params$width <- width if (!missing(height)) params$height <- height @@ -56,7 +68,7 @@ render_png <- function( ) } - httr::stop_for_status(res) + check_or_report_status(res) magick::image_read(httr::content(res, as = "raw")) } \ No newline at end of file diff --git a/R/utils-infix-helpers.R b/R/utils-infix-helpers.R new file mode 100644 index 0000000..11e792d --- /dev/null +++ b/R/utils-infix-helpers.R @@ -0,0 +1,4 @@ +`%l0%` <- function(x, y) if (length(x) == 0) y else x +`%||%` <- function(x, y) if (is.null(x)) y else x +`%@%` <- function(x, name) attr(x, name, exact = TRUE) +`%nin%` <- function(x, table) match(x, table, nomatch = 0) == 0 diff --git a/README.Rmd b/README.Rmd index 6732d6d..b65c4c2 100644 --- a/README.Rmd +++ b/README.Rmd @@ -16,8 +16,8 @@ It's also an alternative to the somewhat abandoned `phantomjs` (which you can us The package uses the [`stevedore`](https://github.com/richfitz/stevedore) package to orchestrate Docker on your system (if you have Docker and more on how to use the `stevedore` integration below) but you can also do get it running in Docker on the command-line with two commands: - sudo docker pull scrapinghub/splash:3.0 - sudo docker run -p 5023:5023 -p 8050:8050 -p 8051:8051 scrapinghub/splash:3.0 + sudo docker pull scrapinghub/splash:latest --disable-browser-caches + sudo docker run -p 5023:5023 -p 8050:8050 -p 8051:8051 scrapinghub/splash:latest --disable-browser-caches Do whatever you Windows ppl do with Docker on your systems to make ^^ work. @@ -37,7 +37,7 @@ and then run: stop_splash(splash_container) -when done. All of that happens on your localhost and you will not need to specify `splash_obj` to many of the `splashr` functions if you're running Splash in this default configuration as long as you use named parameters. You can also use the pre-defined `splash_local` object if you want to use positional parameters. +when done. All of that happens on your `localhost` and you will not need to specify `splash_obj` to many of the `splashr` functions if you're running Splash in this default configuration as long as you use named parameters. You can also use the pre-defined `splash_local` object if you want to use positional parameters. Now, you can run Selenium in Docker, so this is not unique to Splash. But, a Docker context makes it so that you don't have to run or maintain icky Python stuff directly on your system. Leave it in the abandoned warehouse district where it belongs. @@ -84,6 +84,7 @@ Mini-DSL (domain-specific language). These can be used to create a "script" with Helpers: +- `tidy_har`: Turn a gnHARly HAR object into a tidy data frame - `get_body_size`: Retrieve size of content | body | headers - `get_content_sie`: Retrieve size of content | body | headers - `get_content_type` Retrieve or test content type of a HAR request object @@ -164,6 +165,8 @@ You can also profile pages: render_har(url = "http://www.poynter.org/") -> har print(har) + +tidy_har(har) ``` You can use [`HARtools::HARviewer`](https://github.com/johndharrison/HARtools/blob/master/R/HARviewer.R) — which this pkg import/exports — to get view the HAR in an interactive HTML widget. diff --git a/README.md b/README.md index 8218de3..ff22934 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,53 @@ -[![Travis-CI Build Status](https://travis-ci.org/hrbrmstr/splashr.svg?branch=master)](https://travis-ci.org/hrbrmstr/splashr) [![Coverage Status](https://img.shields.io/codecov/c/github/hrbrmstr/splashr/master.svg)](https://codecov.io/github/hrbrmstr/splashr?branch=master) [![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/splashr)](https://cran.r-project.org/package=splashr) [![](http://cranlogs.r-pkg.org/badges/splashr)](http://cran.rstudio.com/web/packages/splashr/index.html) - -# `splashr` : Tools to Work with the 'Splash' JavaScript Rendering Service - -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 which was really engineered for application testing & validation. - -Sometimes, all you need is a page scrape after javascript has been allowed to roam wild and free over meticulously crafted HTML tags. So, this package does not do *everything* Selenium can in pure R (though, the Lua interface is equally as powerful and accessible via R), but if you're just trying to get a page back that needs javascript rendering, this is a nice, lightweight, consistent alternative. - -It's also an alternative to the somewhat abandoned `phantomjs` (which you can use in R within or without a Selenium context as it's it's own webdriver) and it may be useful to compare renderings between this package & `phantomjs`. - -The package uses the [`stevedore`](https://github.com/richfitz/stevedore) package to orchestrate Docker on your system (if you have Docker and more on how to use the `stevedore` integration below) but you can also do get it running in Docker on the command-line with two commands: - - sudo docker pull scrapinghub/splash:3.0 - sudo docker run -p 5023:5023 -p 8050:8050 -p 8051:8051 scrapinghub/splash:3.0 - -Do whatever you Windows ppl do with Docker on your systems to make ^^ work. - -Folks super-new to Docker on Unix-ish platforms should [make sure to do](https://github.com/hrbrmstr/splashr/issues/3#issuecomment-280686494): +[![Travis-CI Build +Status](https://travis-ci.org/hrbrmstr/splashr.svg?branch=master)](https://travis-ci.org/hrbrmstr/splashr) +[![Coverage +Status](https://img.shields.io/codecov/c/github/hrbrmstr/splashr/master.svg)](https://codecov.io/github/hrbrmstr/splashr?branch=master) +[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/splashr)](https://cran.r-project.org/package=splashr) +[![](http://cranlogs.r-pkg.org/badges/splashr)](http://cran.rstudio.com/web/packages/splashr/index.html) + +# `splashr` : Tools to Work with the ‘Splash’ JavaScript Rendering Service + +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 which was really engineered for +application testing & validation. + +Sometimes, all you need is a page scrape after javascript has been +allowed to roam wild and free over meticulously crafted HTML tags. So, +this package does not do *everything* Selenium can in pure R (though, +the Lua interface is equally as powerful and accessible via R), but if +you’re just trying to get a page back that needs javascript rendering, +this is a nice, lightweight, consistent alternative. + +It’s also an alternative to the somewhat abandoned `phantomjs` (which +you can use in R within or without a Selenium context as it’s it’s own +webdriver) and it may be useful to compare renderings between this +package & `phantomjs`. + +The package uses the +[`stevedore`](https://github.com/richfitz/stevedore) package to +orchestrate Docker on your system (if you have Docker and more on how to +use the `stevedore` integration below) but you can also do get it +running in Docker on the command-line with two commands: + + sudo docker pull scrapinghub/splash:latest --disable-browser-caches + sudo docker run -p 5023:5023 -p 8050:8050 -p 8051:8051 scrapinghub/splash:latest --disable-browser-caches + +Do whatever you Windows ppl do with Docker on your systems to make ^^ +work. + +Folks super-new to Docker on Unix-ish platforms should [make sure to +do](https://github.com/hrbrmstr/splashr/issues/3#issuecomment-280686494): sudo groupadd docker sudo usermod -aG docker $USER -(`$USER` is your username and shld be defined for you in the environment) +(`$USER` is your username and shld be defined for you in the +environment) -If using the [`stevedore`](https://github.com/richfitz/stevedore) package you can use the convience wrappers in this pacakge: +If using the [`stevedore`](https://github.com/richfitz/stevedore) +package you can use the convience wrappers in this pacakge: install_splash() splash_container <- start_splash() @@ -31,85 +55,132 @@ If using the [`stevedore`](https://github.com/richfitz/stevedore) package you ca and then run: stop_splash(splash_container) - -when done. All of that happens on your localhost and you will not need to specify `splash_obj` to many of the `splashr` functions if you're running Splash in this default configuration as long as you use named parameters. You can also use the pre-defined `splash_local` object if you want to use positional parameters. -Now, you can run Selenium in Docker, so this is not unique to Splash. But, a Docker context makes it so that you don't have to run or maintain icky Python stuff directly on your system. Leave it in the abandoned warehouse district where it belongs. +when done. All of that happens on your `localhost` and you will not need +to specify `splash_obj` to many of the `splashr` functions if you’re +running Splash in this default configuration as long as you use named +parameters. You can also use the pre-defined `splash_local` object if +you want to use positional parameters. + +Now, you can run Selenium in Docker, so this is not unique to Splash. +But, a Docker context makes it so that you don’t have to run or maintain +icky Python stuff directly on your system. Leave it in the abandoned +warehouse district where it belongs. -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 fun from there! +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 fun from there\! ### About Splash -> '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 packages 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. +> ‘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 +> packages 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_har`: Return information about Splash interaction with a website in [HAR](http://www.softwareishard.com/blog/har-12-spec/) format. -- `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. -- `execute_lua`: Execute a custom rendering script and return a result. -- `splash`: Configure parameters for connecting to a Splash server -- `install_splash`: Retrieve the Docker image for Splash -- `start_splash`: Start a Splash server Docker container -- `stop_splash`: Stop a running a Splash server Docker container - -Mini-DSL (domain-specific language). These can be used to create a "script" without actually scripting in Lua. They are a less-powerful/configurable set of calls than what you can make with a full Lua function but the idea is to have it take care of very common but simple use-cases, like waiting a period of time before capturing a HAR/HTML/PNG image of a site: - -- `splash_plugins`: Enable or disable browser plugins (e.g. Flash). -- `splash_click`: Trigger mouse click event in web page. -- `splash_focus`: Focus on a document element provided by a CSS selector -- `splash_images`: Enable/disable images -- `splash_response_body`: Enable or disable response content tracking. -- `splash_go`: Go to an URL. -- `splash_wait`: Wait for a period time -- `splash_har`: Return information about Splash interaction with a website in HAR format. -- `splash_html`: Return a HTML snapshot of a current page. -- `splash_png`: Return a screenshot of a current page in PNG format. -- `splash_press`: Trigger mouse press event in web page. -- `splash_release`: Trigger mouse release event in web page. -- `splash_send_keys`: Send keyboard events to page context. -- `splash_send_text`: Send text as input to page context, literally, character by character. -- `splash_user_agent`: Overwrite the User-Agent header for all further requests. NOTE: There are many "helper" user agent strings to go with `splash_user_agent`. Look for objects in `splashr` starting with `ua_`. - -`httr` helpers. These help turn various bits of `splashr` objects into `httr`-ish things: - -- `as_req`: Turn a HAR response entry into a working `httr` function you can use to make a request with -- `as_request`: Turn a HAR response entry into an `httr` `response`-like object (i.e. you can use `httr::content()` on it) + - `render_html`: Return the HTML of the javascript-rendered page. + - `render_har`: Return information about Splash interaction with a + website in [HAR](http://www.softwareishard.com/blog/har-12-spec/) + format. + - `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. + - `execute_lua`: Execute a custom rendering script and return a + result. + - `splash`: Configure parameters for connecting to a Splash server + - `install_splash`: Retrieve the Docker image for Splash + - `start_splash`: Start a Splash server Docker container + - `stop_splash`: Stop a running a Splash server Docker container + +Mini-DSL (domain-specific language). These can be used to create a +“script” without actually scripting in Lua. They are a +less-powerful/configurable set of calls than what you can make with a +full Lua function but the idea is to have it take care of very common +but simple use-cases, like waiting a period of time before capturing a +HAR/HTML/PNG image of a site: + + - `splash_plugins`: Enable or disable browser plugins (e.g. Flash). + - `splash_click`: Trigger mouse click event in web page. + - `splash_focus`: Focus on a document element provided by a CSS + selector + - `splash_images`: Enable/disable images + - `splash_response_body`: Enable or disable response content tracking. + - `splash_go`: Go to an URL. + - `splash_wait`: Wait for a period time + - `splash_har`: Return information about Splash interaction with a + website in HAR format. + - `splash_html`: Return a HTML snapshot of a current page. + - `splash_png`: Return a screenshot of a current page in PNG format. + - `splash_press`: Trigger mouse press event in web page. + - `splash_release`: Trigger mouse release event in web page. + - `splash_send_keys`: Send keyboard events to page context. + - `splash_send_text`: Send text as input to page context, literally, + character by character. + - `splash_user_agent`: Overwrite the User-Agent header for all further + requests. NOTE: There are many “helper” user agent strings to go + with `splash_user_agent`. Look for objects in `splashr` starting + with `ua_`. + +`httr` helpers. These help turn various bits of `splashr` objects into +`httr`-ish things: + + - `as_req`: Turn a HAR response entry into a working `httr` function + you can use to make a request with + - `as_request`: Turn a HAR response entry into an `httr` + `response`-like object (i.e. you can use `httr::content()` on it) Helpers: -- `get_body_size`: Retrieve size of content | body | headers -- `get_content_sie`: Retrieve size of content | body | headers -- `get_content_type` Retrieve or test content type of a HAR request object -- `get_headers_size` Retrieve size of content | body | headers -- `is_binary`: Retrieve or test content type of a HAR request object -- `is_content_type`: Retrieve or test content type of a HAR request object -- `is_css`: Retrieve or test content type of a HAR request object -- `is_gif`: Retrieve or test content type of a HAR request object -- `is_html`: Retrieve or test content type of a HAR request object -- `is_javascript`: Retrieve or test content type of a HAR request object -- `is_jpeg`: Retrieve or test content type of a HAR request object -- `is_json`: Retrieve or test content type of a HAR request object -- `is_plain`: Retrieve or test content type of a HAR request object -- `is_png`: Retrieve or test content type of a HAR request object -- `is_svg`: Retrieve or test content type of a HAR request object -- `is_xhr`: Retrieve or test content type of a HAR request object -- `is_xml`: Retrieve or test content type of a HAR request object - -Some functions from `HARtools` are imported/exported and `%>%` is imported/exported. + - `tidy_har`: Turn a gnHARly HAR object into a tidy data frame + - `get_body_size`: Retrieve size of content | body | headers + - `get_content_sie`: Retrieve size of content | body | headers + - `get_content_type` Retrieve or test content type of a HAR request + object + - `get_headers_size` Retrieve size of content | body | headers + - `is_binary`: Retrieve or test content type of a HAR request object + - `is_content_type`: Retrieve or test content type of a HAR request + object + - `is_css`: Retrieve or test content type of a HAR request object + - `is_gif`: Retrieve or test content type of a HAR request object + - `is_html`: Retrieve or test content type of a HAR request object + - `is_javascript`: Retrieve or test content type of a HAR request + object + - `is_jpeg`: Retrieve or test content type of a HAR request object + - `is_json`: Retrieve or test content type of a HAR request object + - `is_plain`: Retrieve or test content type of a HAR request object + - `is_png`: Retrieve or test content type of a HAR request object + - `is_svg`: Retrieve or test content type of a HAR request object + - `is_xhr`: Retrieve or test content type of a HAR request object + - `is_xml`: Retrieve or test content type of a HAR request object + +Some functions from `HARtools` are imported/exported and `%>%` is +imported/exported. ### TODO -Suggest more in a feature req! +Suggest more in a feature req\! -- Implement `render.json` -- Implement "file rendering" -- Implement `execute` (you can script Splash!) -- Add integration with [`HARtools`](https://github.com/johndharrison/HARtools) -- *Possibly* writing R function wrappers to install/start/stop Splash which would also support enabling javascript profiles, request filters and proxy profiles from with R directly, using [`harbor`](https://github.com/wch/harbor) -- Re-implement `render_file()` -- Testing results with all combinations of parameters + - Implement `render.json` + - Implement “file rendering” + - Implement `execute` (you can script Splash\!) + - Add integration with + [`HARtools`](https://github.com/johndharrison/HARtools) + - *Possibly* writing R function wrappers to install/start/stop + Splash which would also support enabling javascript + profiles, request filters and proxy profiles from with R directly, + using [`harbor`](https://github.com/wch/harbor) + - Re-implement `render_file()` + - Testing results with all combinations of parameters ### Installation @@ -124,7 +195,9 @@ install.packages("splashr", repos = c("https://cinc.rud.is/")) ### Usage -NOTE: ALL of these examples assume Splash is running in the default configuraiton on `localhost` (i.e. started with `start_splash()` or the docker example commands) unless otherwise noted. +NOTE: ALL of these examples assume Splash is running in the default +configuraiton on `localhost` (i.e. started with `start_splash()` or the +docker example commands) unless otherwise noted. ``` r library(splashr) @@ -137,7 +210,7 @@ library(tidyverse) packageVersion("splashr") ``` - ## [1] '0.6.0' + ## [1] '0.7.0' ``` r splash_active() @@ -152,37 +225,39 @@ splash_debug() ## List of 7 ## $ active : list() ## $ argcache: int 0 - ## $ fds : int 19 - ## $ leaks :List of 4 - ## ..$ Deferred : int 50 - ## ..$ LuaRuntime: int 1 - ## ..$ QTimer : int 1 - ## ..$ Request : int 1 - ## $ maxrss : int 191004 + ## $ fds : int 59 + ## $ leaks :List of 5 + ## ..$ Deferred : int 20 + ## ..$ DelayedCall: int 3 + ## ..$ LuaRuntime : int 1 + ## ..$ QTimer : int 1 + ## ..$ Request : int 1 + ## $ maxrss : int 219096 ## $ qsize : int 0 ## $ url : chr "http://localhost:8050" ## - attr(*, "class")= chr [1:2] "splash_debug" "list" ## NULL -Notice the difference between a rendered HTML scrape and a non-rendered one: +Notice the difference between a rendered HTML scrape and a non-rendered +one: ``` r render_html(url = "http://marvel.com/universe/Captain_America_(Steve_Rogers)") ``` - ## {xml_document} - ## - ## [1] \n