Activity

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"

page <- read_html(url)

paths_allowed(url)

 www.goodreads.com                      
[1] TRUE
rank <- page %>% 
  html_elements(".number") %>%
  html_text(trim = TRUE)
rank
  [1] "1"   "2"   "3"   "4"   "5"   "6"   "7"   "8"   "9"   "10"  "11"  "12" 
 [13] "13"  "14"  "15"  "16"  "17"  "18"  "19"  "20"  "21"  "22"  "23"  "24" 
 [25] "25"  "26"  "27"  "28"  "29"  "30"  "31"  "32"  "33"  "34"  "35"  "36" 
 [37] "37"  "38"  "39"  "40"  "41"  "42"  "43"  "44"  "45"  "46"  "47"  "48" 
 [49] "49"  "50"  "51"  "52"  "53"  "54"  "55"  "56"  "57"  "58"  "59"  "60" 
 [61] "61"  "62"  "63"  "64"  "65"  "66"  "67"  "68"  "69"  "70"  "71"  "72" 
 [73] "73"  "74"  "75"  "76"  "77"  "78"  "79"  "80"  "81"  "82"  "83"  "84" 
 [85] "85"  "86"  "87"  "88"  "89"  "90"  "91"  "92"  "93"  "94"  "95"  "96" 
 [97] "97"  "98"  "99"  "100"
title <- page %>% 
  html_elements(".bookTitle span") %>%
  html_text(trim = TRUE)
title
  [1] "The Hunger Games (The Hunger Games, #1)"                                        
  [2] "Pride and Prejudice"                                                            
  [3] "To Kill a Mockingbird"                                                          
  [4] "Harry Potter and the Order of the Phoenix (Harry Potter, #5)"                   
  [5] "The Book Thief"                                                                 
  [6] "Twilight (The Twilight Saga, #1)"                                               
  [7] "Animal Farm"                                                                    
  [8] "J.R.R. Tolkien 4-Book Boxed Set: The Hobbit and The Lord of the Rings"          
  [9] "The Chronicles of Narnia (The Chronicles of Narnia, #1-7)"                      
 [10] "The Fault in Our Stars"                                                         
 [11] "The Picture of Dorian Gray"                                                     
 [12] "The Lightning Thief (Percy Jackson and the Olympians, #1)"                      
 [13] "Wuthering Heights"                                                              
 [14] "The Giving Tree"                                                                
 [15] "Harry Potter and the Order of the Phoenix (Harry Potter, #5)"                   
 [16] "The Perks of Being a Wallflower"                                                
 [17] "The Little Prince"                                                              
 [18] "Gone with the Wind"                                                             
 [19] "Jane Eyre"                                                                      
 [20] "The Great Gatsby"                                                               
 [21] "Crime and Punishment"                                                           
 [22] "The Da Vinci Code (Robert Langdon, #2)"                                         
 [23] "Divergent (Divergent, #1)"                                                      
 [24] "Alice’s Adventures in Wonderland / Through the Looking-Glass"                   
 [25] "Les Misérables"                                                                 
 [26] "Anne of Green Gables (Anne of Green Gables, #1)"                                
 [27] "Memoirs of a Geisha"                                                            
 [28] "The Alchemist"                                                                  
 [29] "Lord of the Flies"                                                              
 [30] "Romeo and Juliet"                                                               
 [31] "Brave New World"                                                                
 [32] "1984"                                                                           
 [33] "The Hitchhiker's Guide to the Galaxy (The Hitchhiker's Guide to the Galaxy, #1)"
 [34] "City of Bones (The Mortal Instruments, #1)"                                     
 [35] "Fahrenheit 451"                                                                 
 [36] "Dracula"                                                                        
 [37] "The Help"                                                                       
 [38] "Charlotte’s Web"                                                                
 [39] "Of Mice and Men"                                                                
 [40] "Little Women (Little Women, #1)"                                                
 [41] "The Catcher in the Rye"                                                         
 [42] "Ender’s Game (Ender's Saga, #1)"                                                
 [43] "The Outsiders"                                                                  
 [44] "A Thousand Splendid Suns"                                                       
 [45] "One Hundred Years of Solitude"                                                  
 [46] "A Game of Thrones (A Song of Ice and Fire, #1)"                                 
 [47] "The Secret Garden"                                                              
 [48] "The Princess Bride"                                                             
 [49] "Harry Potter and the Deathly Hallows (Harry Potter, #7)"                        
 [50] "The Time Traveler's Wife"                                                       
 [51] "Frankenstein"                                                                   
 [52] "The Odyssey"                                                                    
 [53] "The Handmaid’s Tale (The Handmaid's Tale, #1)"                                  
 [54] "The Girl With the Dragon Tattoo (Millennium, #1)"                               
 [55] "The Giver (Giver, #1)"                                                          
 [56] "The Kite Runner"                                                                
 [57] "Harry Potter and the Prisoner of Azkaban (Harry Potter, #3)"                    
 [58] "A Wrinkle in Time (Time Quintet, #1)"                                           
 [59] "Dune (Dune, #1)"                                                                
 [60] "Where the Wild Things Are"                                                      
 [61] "Green Eggs and Ham"                                                             
 [62] "The Lovely Bones"                                                               
 [63] "The Adventures of Huckleberry Finn (Adventures of Tom and Huck, #2)"            
 [64] "Life of Pi"                                                                     
 [65] "Lolita"                                                                         
 [66] "A Tale of Two Cities"                                                           
 [67] "Slaughterhouse-Five"                                                            
 [68] "The Bell Jar"                                                                   
 [69] "Matilda"                                                                        
 [70] "Harry Potter and the Philosopher's Stone (Harry Potter, #1)"                    
 [71] "Water for Elephants"                                                            
 [72] "The Stand"                                                                      
 [73] "Catch-22"                                                                       
 [74] "The Adventures of Sherlock Holmes (Sherlock Holmes, #3)"                        
 [75] "Rebecca"                                                                        
 [76] "A Clockwork Orange"                                                             
 [77] "The Pillars of the Earth (Kingsbridge, #1)"                                     
 [78] "Watership Down (Watership Down, #1)"                                            
 [79] "The Color Purple"                                                               
 [80] "Great Expectations"                                                             
 [81] "The Fellowship of the Ring (The Lord of the Rings, #1)"                         
 [82] "Outlander (Outlander, #1)"                                                      
 [83] "Anna Karenina"                                                                  
 [84] "The Elephant Tree"                                                              
 [85] "The Brothers Karamazov"                                                         
 [86] "One Flew Over the Cuckoo’s Nest"                                                
 [87] "To Kill a Mockingbird"                                                          
 [88] "My Sister's Keeper"                                                             
 [89] "The Zombie Room"                                                                
 [90] "A Tree Grows in Brooklyn"                                                       
 [91] "The Road"                                                                       
 [92] "Harry Potter and the Goblet of Fire (Harry Potter, #4)"                         
 [93] "Gone Girl"                                                                      
 [94] "The Golden Compass (His Dark Materials, #1)"                                    
 [95] "And Then There Were None"                                                       
 [96] "Siddhartha"                                                                     
 [97] "Vampire Academy (Vampire Academy, #1)"                                          
 [98] "Don Quixote"                                                                    
 [99] "The Old Man and the Sea"                                                        
[100] "Angela's Ashes (Frank McCourt, #1)"                                             
author <- page %>% 
  html_elements(".authorName span") %>%
  html_text(trim = TRUE)
author
  [1] "Suzanne Collins"              "Jane Austen"                 
  [3] "Harper Lee"                   "J.K. Rowling"                
  [5] "Markus Zusak"                 "Stephenie Meyer"             
  [7] "George Orwell"                "J.R.R. Tolkien"              
  [9] "C.S. Lewis"                   "John  Green"                 
 [11] "Oscar Wilde"                  "Rick Riordan"                
 [13] "Emily Brontë"                 "Shel Silverstein"            
 [15] "J.K. Rowling"                 "Stephen Chbosky"             
 [17] "Antoine de Saint-Exupéry"     "Margaret Mitchell"           
 [19] "Charlotte Brontë"             "F. Scott Fitzgerald"         
 [21] "Fyodor Dostoevsky"            "Dan    Brown"                
 [23] "Veronica Roth"                "Lewis Carroll"               
 [25] "Victor Hugo"                  "L.M. Montgomery"             
 [27] "Arthur Golden"                "Paulo Coelho"                
 [29] "William Golding"              "William Shakespeare"         
 [31] "Aldous Huxley"                "George Orwell"               
 [33] "Douglas Adams"                "Cassandra Clare"             
 [35] "Ray Bradbury"                 "Bram Stoker"                 
 [37] "Kathryn Stockett"             "E.B. White"                  
 [39] "John Steinbeck"               "Louisa May Alcott"           
 [41] "J.D. Salinger"                "Orson Scott Card"            
 [43] "S.E. Hinton"                  "Khaled Hosseini"             
 [45] "Gabriel García Márquez"       "George R.R. Martin"          
 [47] "Frances Hodgson Burnett"      "William Goldman"             
 [49] "J.K. Rowling"                 "Audrey Niffenegger"          
 [51] "Mary Wollstonecraft Shelley"  "Homer"                       
 [53] "Margaret Atwood"              "Stieg Larsson"               
 [55] "Lois Lowry"                   "Khaled Hosseini"             
 [57] "J.K. Rowling"                 "Madeleine L'Engle"           
 [59] "Frank Herbert"                "Maurice Sendak"              
 [61] "Dr. Seuss"                    "Alice Sebold"                
 [63] "Mark Twain"                   "Yann Martel"                 
 [65] "Vladimir Nabokov"             "Charles Dickens"             
 [67] "Kurt Vonnegut Jr."            "Sylvia Plath"                
 [69] "Roald Dahl"                   "J.K. Rowling"                
 [71] "Sara Gruen"                   "Stephen  King"               
 [73] "Joseph Heller"                "Arthur Conan Doyle"          
 [75] "Daphne du Maurier"            "Anthony Burgess"             
 [77] "Ken Follett"                  "Richard  Adams"              
 [79] "Alice Walker"                 "Charles Dickens"             
 [81] "J.R.R. Tolkien"               "Diana Gabaldon"              
 [83] "Leo Tolstoy"                  "R.D. Ronald"                 
 [85] "Fyodor Dostoevsky"            "Ken Kesey"                   
 [87] "Harper Lee"                   "Jodi Picoult"                
 [89] "R.D. Ronald"                  "Betty  Smith"                
 [91] "Cormac McCarthy"              "J.K. Rowling"                
 [93] "Gillian Flynn"                "Philip Pullman"              
 [95] "Agatha Christie"              "Hermann Hesse"               
 [97] "Richelle Mead"                "Miguel de Cervantes Saavedra"
 [99] "Ernest Hemingway"             "Frank McCourt"               
score <- page %>% 
  html_elements(".uitext a:nth-child(1)") %>%
  html_text(trim = TRUE) %>%
  gsub("score:","",.) %>%
  gsub(",","",.) %>%
  as.numeric()
score
  [1] 4567090 3095808 2683165 2129002 2032473 1807409 1797238 1729884 1569766
 [10] 1456683 1429671 1338505 1290202 1271427 1246895 1239093 1231936 1206012
 [19] 1186581 1167085 1108276 1045335 1029380 1028857  989921  987838  981753
 [28]  979881  966740  944982  941775  907464  901488  881938  879561  874047
 [37]  868112  857557  840270  825427  821025  817135  797708  786446  786356
 [46]  746799  745571  742311  720181  712364  705733  696064  680668  677320
 [55]  663878  663021  662958  653019  613324  590696  589101  579067  572733
 [64]  566400  560666  554660  549017  540167  532215  522361  508869  468269
 [73]  462485  452336  446465  446101  445297  436623  425929  422299  419500
 [82]  417216  415813  409750  397118  396711  383196  382257  377187  357978
 [91]  355924  353620  346028  337809  336464  329071  324511  320887  317361
[100]  315498
books_df <- tibble(
  book_rank    = rank,
  book_title    = title,
  book_author = author,
  book_score   = score
)
books_df
# A tibble: 100 × 4
   book_rank book_title                                   book_author book_score
   <chr>     <chr>                                        <chr>            <dbl>
 1 1         The Hunger Games (The Hunger Games, #1)      Suzanne Co…    4567090
 2 2         Pride and Prejudice                          Jane Austen    3095808
 3 3         To Kill a Mockingbird                        Harper Lee     2683165
 4 4         Harry Potter and the Order of the Phoenix (… J.K. Rowli…    2129002
 5 5         The Book Thief                               Markus Zus…    2032473
 6 6         Twilight (The Twilight Saga, #1)             Stephenie …    1807409
 7 7         Animal Farm                                  George Orw…    1797238
 8 8         J.R.R. Tolkien 4-Book Boxed Set: The Hobbit… J.R.R. Tol…    1729884
 9 9         The Chronicles of Narnia (The Chronicles of… C.S. Lewis     1569766
10 10        The Fault in Our Stars                       John  Green    1456683
# ℹ 90 more rows
write_csv(books_df, "top_ranked_books.csv")
getwd()
[1] "/Users/chloelin/Documents/Montgomery College /FALL 2026/DATA 110"