# read_html(url) reads raw html data
# html_element(page) connects entire download webpage and seperates it into 20 columns
# tibble() builds a clean and organized spreadsheet
# list() used to create list
all_books <- list()
for (i in 1:50) {
if (i == 1) {
url <- "https://books.toscrape.com"
} else {
url <- paste0("https://books.toscrape.com/catalogue/page-", i, ".html")
}
page <- read_html(url)
books <- html_elements(page, "article.product_pod")
all_books[[i]] <- tibble(
title = html_attr(html_element(books, "h3 > a"), "title"),
rating = html_attr(html_element(books, "p.star-rating"), "class"),
price = html_text2(html_element(books, "p.price_color")),
stock = html_text2(html_element(books, "p.availability")),
image = html_attr(html_element(books, "img.thumbnail"), "src")
)
Sys.sleep(0.5)
}
full_df <- bind_rows(all_books)