Tools to Work with the 'Splash' JavaScript Rendering Service in R
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.


3 년 전
3 년 전
3 년 전
3 년 전
3 년 전
3 년 전
3 년 전
3 년 전
3 년 전
3 년 전
3 년 전
3 년 전
3 년 전
3 년 전
3 년 전
1 년 전
3 년 전
3 년 전
3 년 전
  1. ---
  2. output: rmarkdown::github_document
  3. ---
  4. [![Travis-CI Build Status](](
  5. [![Coverage Status](](
  6. [![CRAN_Status_Badge](](
  7. [![](](
  8. # `splashr` : Tools to Work with the 'Splash' JavaScript Rendering Service
  9. 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.
  10. 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.
  11. 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`.
  12. The package uses the [`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:
  13. sudo docker pull scrapinghub/splash:3.0
  14. sudo docker run -p 5023:5023 -p 8050:8050 -p 8051:8051 scrapinghub/splash:3.0
  15. Do whatever you Windows ppl do with Docker on your systems to make ^^ work.
  16. Folks super-new to Docker on Unix-ish platforms should [make sure to do](
  17. sudo groupadd docker
  18. sudo usermod -aG docker $USER
  19. (`$USER` is your username and shld be defined for you in the environment)
  20. If using the [`stevedore`]( package you can use the convience wrappers in this pacakge:
  21. install_splash()
  22. splash_container <- start_splash()
  23. and then run:
  24. stop_splash(splash_container)
  25. 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.
  26. 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.
  27. 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!
  28. ### About Splash
  29. >'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.
  30. The following functions are implemented:
  31. - `render_html`: Return the HTML of the javascript-rendered page.
  32. - `render_har`: Return information about Splash interaction with a website in [HAR]( format.
  33. - `render_jpeg`: Return a image (in JPEG format) of the javascript-rendered page.
  34. - `render_png`: Return a image (in PNG format) of the javascript-rendered page.
  35. - `execute_lua`: Execute a custom rendering script and return a result.
  36. - `splash`: Configure parameters for connecting to a Splash server
  37. - `install_splash`: Retrieve the Docker image for Splash
  38. - `start_splash`: Start a Splash server Docker container
  39. - `stop_splash`: Stop a running a Splash server Docker container
  40. 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:
  41. - `splash_plugins`: Enable or disable browser plugins (e.g. Flash).
  42. - `splash_click`: Trigger mouse click event in web page.
  43. - `splash_focus`: Focus on a document element provided by a CSS selector
  44. - `splash_images`: Enable/disable images
  45. - `splash_response_body`: Enable or disable response content tracking.
  46. - `splash_go`: Go to an URL.
  47. - `splash_wait`: Wait for a period time
  48. - `splash_har`: Return information about Splash interaction with a website in HAR format.
  49. - `splash_html`: Return a HTML snapshot of a current page.
  50. - `splash_png`: Return a screenshot of a current page in PNG format.
  51. - `splash_press`: Trigger mouse press event in web page.
  52. - `splash_release`: Trigger mouse release event in web page.
  53. - `splash_send_keys`: Send keyboard events to page context.
  54. - `splash_send_text`: Send text as input to page context, literally, character by character.
  55. - `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_`.
  56. `httr` helpers. These help turn various bits of `splashr` objects into `httr`-ish things:
  57. - `as_req`: Turn a HAR response entry into a working `httr` function you can use to make a request with
  58. - `as_request`: Turn a HAR response entry into an `httr` `response`-like object (i.e. you can use `httr::content()` on it)
  59. Helpers:
  60. - `get_body_size`: Retrieve size of content | body | headers
  61. - `get_content_sie`: Retrieve size of content | body | headers
  62. - `get_content_type` Retrieve or test content type of a HAR request object
  63. - `get_headers_size` Retrieve size of content | body | headers
  64. - `is_binary`: Retrieve or test content type of a HAR request object
  65. - `is_content_type`: Retrieve or test content type of a HAR request object
  66. - `is_css`: Retrieve or test content type of a HAR request object
  67. - `is_gif`: Retrieve or test content type of a HAR request object
  68. - `is_html`: Retrieve or test content type of a HAR request object
  69. - `is_javascript`: Retrieve or test content type of a HAR request object
  70. - `is_jpeg`: Retrieve or test content type of a HAR request object
  71. - `is_json`: Retrieve or test content type of a HAR request object
  72. - `is_plain`: Retrieve or test content type of a HAR request object
  73. - `is_png`: Retrieve or test content type of a HAR request object
  74. - `is_svg`: Retrieve or test content type of a HAR request object
  75. - `is_xhr`: Retrieve or test content type of a HAR request object
  76. - `is_xml`: Retrieve or test content type of a HAR request object
  77. Some functions from `HARtools` are imported/exported and `%>%` is imported/exported.
  78. ### TODO
  79. Suggest more in a feature req!
  80. - <strike>Implement `render.json`</strike>
  81. - <strike>Implement "file rendering"</strike>
  82. - <strike>Implement `execute` (you can script Splash!)</strike>
  83. - <strike>Add integration with [`HARtools`](</strike>
  84. - <strike>_Possibly_ writing R function wrappers to install/start/stop Splash</strike> which would also support enabling javascript profiles, request filters and proxy profiles from with R directly, using [`harbor`](
  85. - Re-implement `render_file()`
  86. - Testing results with all combinations of parameters
  87. ### Installation
  88. ```{r eval=FALSE}
  89. devtools::install_git("")
  90. # or
  91. devtools::install_git("")
  92. # or
  93. devtools::install_github("hrbrmstr/splashr")
  94. ```
  95. ```{r message=FALSE, warning=FALSE, error=FALSE, include=FALSE}
  96. options(width=120)
  97. ```
  98. ### Usage
  99. 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.
  100. ```{r message=FALSE, warning=FALSE, error=FALSE}
  101. library(splashr)
  102. library(magick)
  103. library(rvest)
  104. library(anytime)
  105. library(tidyverse)
  106. # current verison
  107. packageVersion("splashr")
  108. splash_active()
  109. splash_debug()
  110. ```
  111. Notice the difference between a rendered HTML scrape and a non-rendered one:
  112. ```{r}
  113. render_html(url = "")
  114. xml2::read_html("")
  115. ```
  116. You can also profile pages:
  117. ```{r}
  118. render_har(url = "") -> har
  119. print(har)
  120. ```
  121. You can use [`HARtools::HARviewer`]( — which this pkg import/exports — to get view the HAR in an interactive HTML widget.
  122. Full web page snapshots are easy-peasy too:
  123. ```{r eval=FALSE}
  124. render_png(url = "")
  125. ```
  126. ```{r eval=TRUE, include=FALSE}
  127. render_png(url = "") %>%
  128. image_write("img/cap.png")
  129. ```
  130. ![](img/cap.png)
  131. ```{r eval=FALSE}
  132. render_jpeg(url = "")
  133. ```
  134. ```{r eval=TRUE, include=FALSE}
  135. render_jpeg(url = "") %>%
  136. image_write("img/cap.jpg")
  137. ```
  138. ![](img/cap.jpg)
  139. ### Executing custom Lua scripts
  140. ```{r}
  141. lua_ex <- '
  142. function main(splash)
  143. splash:go("")
  144. splash:wait(0.5)
  145. local title = splash:evaljs("document.title")
  146. return {title=title}
  147. end
  148. '
  149. splash_local %>% execute_lua(lua_ex) -> res
  150. rawToChar(res) %>%
  151. jsonlite::fromJSON()
  152. ```
  153. ### Interacting With Flash sites
  154. ```{r eval=FALSE}
  155. splash_local %>%
  156. splash_plugins(TRUE) %>%
  157. splash_go("") %>%
  158. splash_wait(4) %>%
  159. splash_click(460, 550) %>%
  160. splash_wait(2) %>%
  161. splash_click(230, 85) %>%
  162. splash_wait(2) %>%
  163. splash_png()
  164. ```
  165. <img src="img/flash.png" width="50%"/>
  166. ```{r eval=FALSE}
  167. stop_splash(splash_vm)
  168. ```
  169. ### Code of Conduct
  170. Please note that this project is released with a [Contributor Code of Conduct]( By participating in this project you agree to abide by its terms.