Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix read_html_live example #426

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions R/live.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,27 @@
#' # When we retrieve the raw HTML for this site, it doesn't contain the
#' # data we're interested in:
#' static <- read_html("https://www.forbes.com/top-colleges/")
#' static %>% html_elements(".TopColleges2023_tableRow__BYOSU")
#' static %>% html_elements(".ListTable_listTable__-N5U5")
#'
#' # Instead, we need to run the site in a real web browser, causing it to
#' # download a JSON file and then dynamically generate the html:
#'
#' sess <- read_html_live("https://www.forbes.com/top-colleges/")
#' sess$view()
#' rows <- sess %>% html_elements(".TopColleges2023_tableRow__BYOSU")
#' rows %>% html_element(".TopColleges2023_organizationName__J1lEV") %>% html_text()
#' rows %>% html_element(".grant-aid") %>% html_text()
#' Sys.sleep(2)
#' cookies_seen <- length(html_elements(sess, "button[aria-label='Reject All']"))
#' if (cookies_seen) {
#' sess$click("button[aria-label='Reject All']")
#' sess <- read_html_live("https://www.forbes.com/top-colleges/")
#' sess$view()
#' Sys.sleep(2)
#' }
#' table_seen <- length(html_elements(sess, "table"))
#' if (table_seen) {
#' rows <- sess |> html_elements("table tr")
#' rows |>
#' html_elements("td:nth-of-type(2)") |>
#' html_text() |>
#' print()
#' }
read_html_live <- function(url) {
check_installed(c("chromote", "R6"))
Expand Down
Loading