Scrapping_assignment

Author

Asim Maharjan

# Loading libraries
library(rvest)
Warning: package 'rvest' was built under R version 4.6.1
library(readr)
Warning: package 'readr' was built under R version 4.6.1

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

    guess_encoding
library(dplyr)
Warning: package 'dplyr' was built under R version 4.6.1

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
# Reading website data and checking if we can scrap or not
urls <- ("https://www.goodreads.com/list/show/1.Best_Books_Ever")
book <- read_html(urls)

library(robotstxt)
Warning: package 'robotstxt' was built under R version 4.6.1
paths_allowed(urls)

 www.goodreads.com                      
[1] TRUE
# Scrapping title of the book from the website
title <- book |>
  html_elements(".bookTitle span") |>
  html_text(trim = TRUE) |>
  gsub("\\(.*\\)", "", x = _) 
head(title)
[1] "The Hunger Games "                         
[2] "Pride and Prejudice"                       
[3] "To Kill a Mockingbird"                     
[4] "Harry Potter and the Order of the Phoenix "
[5] "The Book Thief"                            
[6] "Twilight "                                 
# Scrapping rank of the book from the website
rank <- book |>
  html_elements(".number") |>
  html_text(trim = TRUE)
head(rank)
[1] "1" "2" "3" "4" "5" "6"
# Scrapping author of the book from the website
author <- book |>
  html_elements(".authorName span") |>
  html_text(trim = TRUE)
head(author)
[1] "Suzanne Collins" "Jane Austen"     "Harper Lee"      "J.K. Rowling"   
[5] "Markus Zusak"    "Stephenie Meyer"
# Scrapping score of the book from the website and removig unnecessary fonts
score <- book |>
  html_elements(".uitext a:nth-child(1)") |>
  html_text(trim = TRUE) |>
   gsub("score: ", "", x = _) |>
   gsub(",", "", x = _) |>
  as.numeric()
head(score)
[1] 4567589 3096309 2683365 2129099 2032570 1807509
# Creating my own data-set and storing above data with its name in my_books variable.
my_books <- tibble(
  rank = rank,
  title = title,
  author = author,
  score = score
)
# Writing csv file.
my_books 
# A tibble: 100 × 4
   rank  title                                                     author  score
   <chr> <chr>                                                     <chr>   <dbl>
 1 1     "The Hunger Games "                                       Suzan… 4.57e6
 2 2     "Pride and Prejudice"                                     Jane … 3.10e6
 3 3     "To Kill a Mockingbird"                                   Harpe… 2.68e6
 4 4     "Harry Potter and the Order of the Phoenix "              J.K. … 2.13e6
 5 5     "The Book Thief"                                          Marku… 2.03e6
 6 6     "Twilight "                                               Steph… 1.81e6
 7 7     "Animal Farm"                                             Georg… 1.80e6
 8 8     "J.R.R. Tolkien 4-Book Boxed Set: The Hobbit and The Lor… J.R.R… 1.73e6
 9 9     "The Chronicles of Narnia "                               C.S. … 1.57e6
10 10    "The Fault in Our Stars"                                  John … 1.46e6
# ℹ 90 more rows
write_csv(my_books, "books.csv")