Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(tidyverse)
- library(rvest)
- hp_link <- "https://www.mountainproject.com/"
- hp_links <- read_html(hp_link) |>
- html_nodes("a") |>
- html_attr("href")
- route_links <- hp_links |>
- keep(str_detect(hp_links, "/route/")) |>
- unique() |>
- head(-1)
- base <- route_links |>
- str_split("/route/") |>
- pluck(1) |>
- pluck(1) |>
- paste0("/route/stats")
- stat_pages <- route_links |>
- str_split("/route") |>
- map_chr(pluck(2)) |>
- (\(x) paste0(base, x))()
- get_users <- function(link) {
- route <- link |>
- read_html() |>
- html_nodes("table") |>
- pluck(4) |>
- html_nodes("a") |>
- html_attr("href") |>
- str_extract("user/\\d+") |>
- str_remove("user/")
- route
- }
- users <- map(stat_pages, get_users)
- route_names <- route_links |>
- str_extract("\\d+/.+") |>
- str_remove("\\d+/")
- users |>
- map(as.data.frame) |>
- setNames(route_names) |>
- bind_rows(.id = "route") |>
- setNames(c("route", "user")) |>
- mutate(user = as.numeric(user)) |>
- pivot_wider(names_from = user, values_from = route, values_fn = length) |>
- pivot_longer(everything(), names_to = "user", values_to = "count") |>
- View()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement