In Class Activity

In class 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%20Books_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"  "44"  "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) %>%
  gsub("#","", .) %>%
  gsub("1","", .) %>%
  gsub("2","", . ) %>%
  gsub("3","",.) %>%
  gsub("4","", .) %>%
  gsub("5","", .) %>%
  gsub("7","", .) %>%
  gsub(",","", .)
title
  [1] "The Hunger Games (The Hunger Games )"                                        
  [2] "Pride and Prejudice"                                                         
  [3] "To Kill a Mockingbird"                                                       
  [4] "Harry Potter and the Order of the Phoenix (Harry Potter )"                   
  [5] "The Book Thief"                                                              
  [6] "Twilight (The Twilight Saga )"                                               
  [7] "Animal Farm"                                                                 
  [8] "J.R.R. Tolkien -Book Boxed Set: The Hobbit and The Lord of the Rings"        
  [9] "The Chronicles of Narnia (The Chronicles of Narnia -)"                       
 [10] "The Fault in Our Stars"                                                      
 [11] "The Picture of Dorian Gray"                                                  
 [12] "The Lightning Thief (Percy Jackson and the Olympians )"                      
 [13] "Wuthering Heights"                                                           
 [14] "The Giving Tree"                                                             
 [15] "Harry Potter and the Order of the Phoenix (Harry Potter )"                   
 [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 )"                                         
 [23] "Divergent (Divergent )"                                                      
 [24] "Alice’s Adventures in Wonderland / Through the Looking-Glass"                
 [25] "Les Misérables"                                                              
 [26] "Anne of Green Gables (Anne of Green Gables )"                                
 [27] "Memoirs of a Geisha"                                                         
 [28] "The Alchemist"                                                               
 [29] "Lord of the Flies"                                                           
 [30] "Romeo and Juliet"                                                            
 [31] "Brave New World"                                                             
 [32] "98"                                                                          
 [33] "The Hitchhiker's Guide to the Galaxy (The Hitchhiker's Guide to the Galaxy )"
 [34] "City of Bones (The Mortal Instruments )"                                     
 [35] "Fahrenheit "                                                                 
 [36] "Dracula"                                                                     
 [37] "The Help"                                                                    
 [38] "Charlotte’s Web"                                                             
 [39] "Of Mice and Men"                                                             
 [40] "Little Women (Little Women )"                                                
 [41] "The Catcher in the Rye"                                                      
 [42] "Ender’s Game (Ender's Saga )"                                                
 [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 )"                                 
 [47] "The Secret Garden"                                                           
 [48] "The Princess Bride"                                                          
 [49] "Harry Potter and the Deathly Hallows (Harry Potter )"                        
 [50] "The Time Traveler's Wife"                                                    
 [51] "Frankenstein"                                                                
 [52] "The Odyssey"                                                                 
 [53] "The Handmaid’s Tale (The Handmaid's Tale )"                                  
 [54] "The Girl With the Dragon Tattoo (Millennium )"                               
 [55] "The Giver (Giver )"                                                          
 [56] "The Kite Runner"                                                             
 [57] "Harry Potter and the Prisoner of Azkaban (Harry Potter )"                    
 [58] "A Wrinkle in Time (Time Quintet )"                                           
 [59] "Dune (Dune )"                                                                
 [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 )"            
 [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 )"                    
 [71] "Water for Elephants"                                                         
 [72] "The Stand"                                                                   
 [73] "Catch-"                                                                      
 [74] "The Adventures of Sherlock Holmes (Sherlock Holmes )"                        
 [75] "Rebecca"                                                                     
 [76] "A Clockwork Orange"                                                          
 [77] "The Pillars of the Earth (Kingsbridge )"                                     
 [78] "Watership Down (Watership Down )"                                            
 [79] "The Color Purple"                                                            
 [80] "Great Expectations"                                                          
 [81] "The Fellowship of the Ring (The Lord of the Rings )"                         
 [82] "Outlander (Outlander )"                                                      
 [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 )"                         
 [93] "Gone Girl"                                                                   
 [94] "The Golden Compass (His Dark Materials )"                                    
 [95] "And Then There Were None"                                                    
 [96] "Siddhartha"                                                                  
 [97] "Vampire Academy (Vampire Academy )"                                          
 [98] "Don Quixote"                                                                 
 [99] "The Old Man and the Sea"                                                     
[100] "Angela's Ashes (Frank McCourt )"                                             
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: ", "", .)
score
  [1] "4,567,889" "3,096,409" "2,683,464" "2,129,241" "2,032,867" "1,807,608"
  [7] "1,797,534" "1,730,485" "1,570,062" "1,456,683" "1,429,865" "1,338,999"
 [13] "1,290,302" "1,271,627" "1,247,092" "1,239,210" "1,232,428" "1,206,012"
 [19] "1,187,167" "1,167,566" "1,108,565" "1,045,335" "1,029,579" "1,028,859"
 [25] "990,028"   "988,224"   "981,848"   "980,079"   "966,923"   "944,982"  
 [31] "941,869"   "908,145"   "901,488"   "881,938"   "879,949"   "874,432"  
 [37] "868,112"   "857,746"   "840,370"   "825,714"   "821,227"   "817,232"  
 [43] "797,898"   "786,446"   "786,446"   "746,897"   "745,755"   "742,495"  
 [49] "720,471"   "712,364"   "705,744"   "696,151"   "680,955"   "677,320"  
 [55] "664,430"   "663,213"   "663,056"   "653,019"   "613,423"   "590,696"  
 [61] "589,277"   "579,165"   "572,818"   "566,301"   "560,666"   "554,744"  
 [67] "549,103"   "540,360"   "532,401"   "522,460"   "508,869"   "468,269"  
 [73] "462,485"   "452,419"   "446,646"   "445,923"   "445,297"   "436,620"  
 [79] "426,017"   "422,379"   "419,674"   "417,310"   "415,891"   "409,750"  
 [85] "397,294"   "396,711"   "383,359"   "382,353"   "377,187"   "357,978"  
 [91] "355,999"   "353,620"   "346,028"   "337,902"   "336,462"   "329,071"  
 [97] "324,511"   "321,085"   "317,435"   "315,498"  
books_df <- tibble(
  Ranks   = rank,
  Titles    = title,
  Authors = author,
  Scores   = score,
)
write_csv(books_df, "book_ranks_page.csv")
getwd()
[1] "C:/Users/solei/OneDrive/Documents"