-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
106 lines (71 loc) · 2.57 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
---
output:
rmarkdown::github_document:
df_print: kable
---
```{r pkg-knitr-opts, include=FALSE}
hrbrpkghelpr::global_opts()
```
```{r badges, results='asis', echo=FALSE, cache=FALSE}
hrbrpkghelpr::stinking_badges()
```
```{r description, results='asis', echo=FALSE, cache=FALSE}
hrbrpkghelpr::yank_title_and_description()
```
>_`HtmlUnit` is a "GUI-Less browser for Java programs". It models HTML documents and provides an API that allows you to invoke pages, fill out forms, click links, etc... just like you do in your "normal" browser._
>
>_It has fairly good JavaScript support (which is constantly improving) and is able to work even with quite complex AJAX libraries, simulating Chrome, Firefox or Internet Explorer depending on the configuration used._
>
>_It is typically used for testing purposes or to retrieve information from web sites._
>
>_`HtmlUnit` is not a generic unit testing framework. It is specifically a way to simulate a browser._
## What's Inside The Tin
Everything necessary to use the HtmlUnit library directly via `rJava`.
`HtmlUnit` Library JavaDoc: <https://htmlunit.sourceforge.net/apidocs/index.html>
## Installation
```{r install-ex, results='asis', echo=FALSE, cache=FALSE}
hrbrpkghelpr::install_block()
```
## Usage
```{r lib, cache=FALSE}
library(htmlunitjars)
# current verison
packageVersion("htmlunitjars")
```
### Give It A Go
`xml2::read_html()` cannot execute javascript so the traditional approach won't work:
```{r go1}
library(rvest)
test_url <- "https://hrbrmstr.github.io/htmlunitjars/index.html"
doc <- read_html(test_url)
html_table(doc)
```
☹️
We _can_ do this with the classes from `HtmlUnit` proivided by this JAR wrapper package:
```{r go2}
library(htmlunitjars)
```
Tell `HtmlUnit` to work like FireFox:
```{r go3}
browsers <- J("com.gargoylesoftware.htmlunit.BrowserVersion")
wc <- new(J("com.gargoylesoftware.htmlunit.WebClient"), browsers$CHROME)
```
Tell it to wait for javascript to execute and not throw exceptions on page resource errors:
```{r go4}
invisible(wc$waitForBackgroundJavaScriptStartingBefore(.jlong(2000L)))
wc_opts <- wc$getOptions()
wc_opts$setThrowExceptionOnFailingStatusCode(FALSE)
wc_opts$setThrowExceptionOnScriptError(FALSE)
```
Now, acccess the site again and get the table:
```{r go5}
pg <- wc$getPage(test_url)
doc <- read_html(pg$asXml())
html_table(doc)
```
No need for Selenium or Splash!
The ultimate goal is to have an `htmlunit` package that provides a nicer API than needing to know how to work with `rJava` directly.
## htmlunitjars Metrics
```{r cloc, echo=FALSE}
cloc::cloc_pkg_md()
```