# Load packages
library(rvest)
## Loading required package: xml2
library(stringr)
library(dplyr)
## 
## 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
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(readr)
## 
## Attaching package: 'readr'
## The following object is masked from 'package:rvest':
## 
##     guess_encoding
# Read web page
webpage <- read_html("https://www.goodreads.com/author/quotes/7221234.Elon_Musk")

# Extract records info
results <- webpage %>% html_nodes(".quoteText")

# Building the dataset
records <- vector("list", length = length(results))

for (i in seq_along(results)) {
  author <- str_c(results[i] %>% 
                  html_nodes(".authorOrTitle") %>% 
                  html_text(trim = TRUE))
  quote <- str_sub(xml_contents(results[i])[1] %>% html_text(trim = TRUE), 2, -2)
  records[[i]] <- data_frame(author = author, quote = quote)
}
## Warning: `data_frame()` is deprecated as of tibble 1.1.0.
## Please use `tibble()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
df <- bind_rows(records)
df
## # A tibble: 32 x 2
##    author    quote                                                              
##    <chr>     <chr>                                                              
##  1 Elon Musk When something is important enough, you do it even if the odds are~
##  2 Elon Musk It is important to view knowledge as sort of a semantic tree -- ma~
##  3 Elon Musk My proceeds from the PayPal acquisition were $180 million. I put $~
##  4 Elon Musk You should take the approach that you’re wrong. Your goal is to be~
##  5 Elon Musk You get paid in direct proportion to the difficulty of problems yo~
##  6 Elon Musk I think it's very important to have a feedback loop, where you're ~
##  7 Elon Musk I think it’s important to reason from first principles rather than~
##  8 Elon Musk I think it would be great to be born on Earth and die on Mars. Jus~
##  9 Elon Musk Constantly seek criticism. A well thought out critique of whatever~
## 10 Elon Musk It is possible for ordinary people to choose to be extraordinary.  
## # ... with 22 more rows
# Export to Excel sheet
library(writexl)
## Warning: package 'writexl' was built under R version 4.0.5
write_xlsx(df, "em.xlsx")
# Export to csv
write_csv(df, "elon_quotes.csv")