CSS web scrapping activity

Text extracting –> to dataset

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(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
Title <- page %>%
  html_elements(".bookTitle span") %>%
  html_text(trim = TRUE) # get text, remove spaces
Author <- page %>%
  html_elements(".authorName span") %>%
  html_text(trim = TRUE)
Rank <- page %>%
  html_elements(".number") %>%
  html_text(trim = TRUE) %>%
  as.numeric()
Score <- page %>%
  html_elements(".uitext a:nth-child(1)") %>%
  html_text(trim = TRUE) %>%
  gsub("score: ","", .) %>%
  gsub(",","", .)
#or
#parse_number(score)
#make data frame
Books_df <- tibble(
  Title = Title,
  Author = Author,
  Rank = Rank,
  Score = Score)
Books_df
# A tibble: 100 × 4
   Title                                                      Author  Rank Score
   <chr>                                                      <chr>  <dbl> <chr>
 1 The Hunger Games (The Hunger Games, #1)                    Suzan…     1 4566…
 2 Pride and Prejudice                                        Jane …     2 3095…
 3 To Kill a Mockingbird                                      Harpe…     3 2683…
 4 Harry Potter and the Order of the Phoenix (Harry Potter, … J.K. …     4 2129…
 5 The Book Thief                                             Marku…     5 2032…
 6 Twilight (The Twilight Saga, #1)                           Steph…     6 1807…
 7 Animal Farm                                                Georg…     7 1797…
 8 J.R.R. Tolkien 4-Book Boxed Set: The Hobbit and The Lord … J.R.R…     8 1729…
 9 The Chronicles of Narnia (The Chronicles of Narnia, #1-7)  C.S. …     9 1569…
10 The Fault in Our Stars                                     John …    10 1456…
# ℹ 90 more rows
Books_df
# A tibble: 100 × 4
   Title                                                      Author  Rank Score
   <chr>                                                      <chr>  <dbl> <chr>
 1 The Hunger Games (The Hunger Games, #1)                    Suzan…     1 4566…
 2 Pride and Prejudice                                        Jane …     2 3095…
 3 To Kill a Mockingbird                                      Harpe…     3 2683…
 4 Harry Potter and the Order of the Phoenix (Harry Potter, … J.K. …     4 2129…
 5 The Book Thief                                             Marku…     5 2032…
 6 Twilight (The Twilight Saga, #1)                           Steph…     6 1807…
 7 Animal Farm                                                Georg…     7 1797…
 8 J.R.R. Tolkien 4-Book Boxed Set: The Hobbit and The Lord … J.R.R…     8 1729…
 9 The Chronicles of Narnia (The Chronicles of Narnia, #1-7)  C.S. …     9 1569…
10 The Fault in Our Stars                                     John …    10 1456…
# ℹ 90 more rows
#save to CSV (Excel-ready)
write_csv(Books_df, "Books_rating.csv")

#to check where it was saved
getwd()
[1] "C:/Users/nawel/OneDrive/Desktop/Data 110"