HTML
url <- "https://raw.githubusercontent.com/curdferguson/607-wk7-hw7/main/recommended_reads_cheese.html"
books_parsed <- read_html(url)
# read the information from the HTML table
books_html <- books_parsed %>%
html_table(fill = TRUE) %>%
.[[1]] %>%
as_tibble() %>%
rename(`Link Text` = `Buy Online`)
# create a column for the link url using nodes and list apply functions
x <- nrow(books_html) + 1
y <- c(2:x)
xpaths <- paste("/html/body/table/tr[", y, "]/td[4]/a", sep="")
nodes <- lapply(xpaths, function(z){
html_node(books_parsed, xpath = z)})
link_urls = lapply(nodes, function(i){
str_match(i, "(?<=(<a href=\")).*(?=(\">[A-Z]))")[,1]})
books_html$`Link Url` <- as_vector(link_urls)
books_html
## # A tibble: 4 x 7
## ISBN Title Author `Link Text` Price Comment `Link Url`
## <dbl> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 9.78e12 Composing t~ Brian ~ Amazon $22.~ "These recip~ https://www.ama~
## 2 9.78e12 HOT CHEESE:~ Polina~ Book Larder $19.~ "From classi~ https://www.boo~
## 3 9.78e12 Cheese and ~ Paul K~ Strand Boo~ $24.~ "A fascinati~ https://www.str~
## 4 9.78e12 The Book of~ Liz Th~ Amazon $40.~ "With its en~ https://www.ama~
JSON
books_json <- jsonlite::fromJSON("https://raw.githubusercontent.com/curdferguson/607-wk7-hw7/main/recommended_reads_cheese.json")
books_json_tibble <- bind_rows(lapply(books_json[[1]], function(x) {
tibble(
ISBN = as.numeric(x$ISBN),
Title = x$Title,
Author = x$Author,
url = x$`Buy Online`[[1]],
`Link Text` = x$`Buy Online`[[2]],
Price = x$Price,
Comment = x$Comment)
}))
books_json_tibble
## # A tibble: 5 x 7
## ISBN Title Author url `Link Text` Price Comment
## <dbl> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 9.78e12 Composing th~ Brian K~ https://www.~ Amazon $22.~ These recipes~
## 2 9.78e12 Composing th~ Leigh F~ https://www.~ Amazon $22.~ These recipes~
## 3 9.78e12 HOT CHEESE: ~ Polina ~ https://www.~ Book Larder $19.~ From classics~
## 4 9.78e12 Cheese and C~ Paul Ki~ https://www.~ Strand Boo~ $24.~ A fascinating~
## 5 9.78e12 The Book of ~ Liz Tho~ https://www.~ Amazon $40.~ With its enga~