Activity week 8

# install.packages(c("tidyverse", "dplyr", "rvest","readr","robotstxt"))

library(tidyverse)   
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(rvest)

Attaching package: 'rvest'

The following object is masked from 'package:readr':

    guess_encoding
library(jsonlite)

Attaching package: 'jsonlite'

The following object is masked from 'package:purrr':

    flatten
library(robotstxt)
url <- "https://www.goodreads.com/list/show/1.Best_Books_Ever"

# read HTML
page <- read_html(url)

#check permission
paths_allowed(url)

 www.goodreads.com                      
[1] TRUE
#extract fields
rank <- page %>%
  html_elements(".number") %>%
  html_text(trim = TRUE) # get text, remove spaces

title <- page %>%
  html_elements(".bookTitle span") %>%
  html_text(trim = TRUE)

author_name <- page %>%
  html_elements(".authorName span") %>%
  html_text(trim = TRUE) %>%
  gsub(",", "", .)  # replace commas with nothing 
  

score <- page %>%
  html_elements(".uitext a:nth-child(1)") %>%
  html_text(trim = TRUE) %>%
  gsub("score:", ",", .) %>%
  gsub(",", "", .) 

#make data frame
books_df <- tibble(
  rank    = rank,
  title    = title,
  author_name = author_name,
  score   = score
)
books_df
# A tibble: 100 × 4
   rank  title                                                 author_name score
   <chr> <chr>                                                 <chr>       <chr>
 1 1     The Hunger Games (The Hunger Games, #1)               Suzanne Co… " 45…
 2 2     Pride and Prejudice                                   Jane Austen " 30…
 3 3     To Kill a Mockingbird                                 Harper Lee  " 26…
 4 4     Harry Potter and the Order of the Phoenix (Harry Pot… J.K. Rowli… " 21…
 5 5     The Book Thief                                        Markus Zus… " 20…
 6 6     Twilight (The Twilight Saga, #1)                      Stephenie … " 18…
 7 7     Animal Farm                                           George Orw… " 17…
 8 8     J.R.R. Tolkien 4-Book Boxed Set: The Hobbit and The … J.R.R. Tol… " 17…
 9 9     The Chronicles of Narnia (The Chronicles of Narnia, … C.S. Lewis  " 15…
10 10    The Fault in Our Stars                                John  Green " 14…
# ℹ 90 more rows
#save to CSV (Excel-ready)
write_csv(books_df, "books_simple_page.csv")

#to check where it was saved
getwd()
[1] "C:/Users/loren/OneDrive/New folder"