#### 1.1. Load packages ####
library(tidyverse) # The tidyverse collection of packages
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 4.0.0 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.1.0
## ── 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(httr) # Useful for web authentication
library(rvest) # Useful tools for working with HTML and XML
##
## Attaching package: 'rvest'
##
## The following object is masked from 'package:readr':
##
## guess_encoding
library(lubridate) # Working with dates
library(magrittr) # Piping output easily with loops
##
## Attaching package: 'magrittr'
##
## The following object is masked from 'package:purrr':
##
## set_names
##
## The following object is masked from 'package:tidyr':
##
## extract
#### Intro ####
# I will be scraping reviews off of rate my professor. I will be answering
# who has the best average rating among professors: Joel Asay, Greg Smith, Mark Sena.
# To complete this i will scrape rate my professor reviews and sort them into
# a data frame
#### 1.2. Define user agent ####
# Defining the user-agent to imitate a Firefox bot
set_config(user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:145.0) Gecko/20100101 Firefox/145.0"))
#### 1.3 Connecting to Rate my Professor ####
rmp_html <-
read_html("https://www.ratemyprofessors.com/professor/2043993")
#### 1.4 Scraping column values like professor, class, rating etc ####
professor <-
rmp_html %>%
html_elements("div.MiniStickyHeader__MiniNameWrapper-sc-122ii22-9") %>%
html_text2()
course <-
rmp_html %>%
html_elements("div.Rating__RatingInfo-sc-1rhvpxz-3 div.RatingHeader__StyledClass-sc-1dlkqw1-3") %>%
html_text2()
rating <-
rmp_html %>%
html_elements("div.RatingValues__RatingContainer-sc-6dc747-1:nth-child(1) div.CardNumRating__CardNumRatingNumber-sc-17t4b9u-2") %>%
html_text2()
course_difficulty <-
rmp_html %>%
html_elements("div.RatingValues__RatingContainer-sc-6dc747-1:nth-child(2) div.CardNumRating__CardNumRatingNumber-sc-17t4b9u-2") %>%
html_text2()
date <-
rmp_html %>%
html_elements("div.Rating__RatingInfo-sc-1rhvpxz-3 div.RatingHeader__RatingTimeStamp-sc-1dlkqw1-4") %>%
html_text2()
review <-
rmp_html %>%
html_elements("div.Comments__StyledComments-dzzyvm-0") %>%
html_text2()
rmp_df <-
tibble(professor, date, course, course_difficulty, rating, review)
#### Converting date column to legible date and rating to numeric ####
rmp_df <-
rmp_df %>%
mutate(date = str_replace_all(date, ",", "")) %>%
mutate(date = str_replace_all(date, "st","")) %>%
mutate(date = str_replace_all(date, "nd","")) %>%
mutate(date = str_replace_all(date, "th","")) %>%
mutate(rating = as.numeric(rating)) %>%
mutate(course_difficulty = as.numeric(course_difficulty)) %>%
mutate(date = mdy(date))
#### Function to Scrape Page ####
rmp_scrape_page <- function(url) {
#### 1.3 Connecting to Rate my Professor ####
rmp_html <-
read_html(url)
#### 1.4 Scraping column values like professor, class, rating etc ####
professor <-
rmp_html %>%
html_elements("div.MiniStickyHeader__MiniNameWrapper-sc-122ii22-9") %>%
html_text2()
course <-
rmp_html %>%
html_elements("div.Rating__RatingInfo-sc-1rhvpxz-3 div.RatingHeader__StyledClass-sc-1dlkqw1-3") %>%
html_text2()
rating <-
rmp_html %>%
html_elements("div.RatingValues__RatingContainer-sc-6dc747-1:nth-child(1) div.CardNumRating__CardNumRatingNumber-sc-17t4b9u-2") %>%
html_text2()
course_difficulty <-
rmp_html %>%
html_elements("div.RatingValues__RatingContainer-sc-6dc747-1:nth-child(2) div.CardNumRating__CardNumRatingNumber-sc-17t4b9u-2") %>%
html_text2()
date <-
rmp_html %>%
html_elements("div.Rating__RatingInfo-sc-1rhvpxz-3 div.RatingHeader__RatingTimeStamp-sc-1dlkqw1-4") %>%
html_text2()
review <-
rmp_html %>%
html_elements("div.Comments__StyledComments-dzzyvm-0") %>%
html_text2()
rmp_df <-
tibble(professor, date, course, course_difficulty, rating, review)
#### Converting date column to legible date ####
rmp_df <-
rmp_df %>%
mutate(date = str_replace_all(date, ",", "")) %>%
mutate(date = str_replace_all(date, "st","")) %>%
mutate(date = str_replace_all(date, "nd","")) %>%
mutate(date = str_replace_all(date, "th","")) %>%
mutate(rating = as.numeric(rating)) %>%
mutate(course_difficulty = as.numeric(course_difficulty)) %>%
mutate(date = mdy(date))
return(rmp_df)
}
#### Looping through different professors URLs ####
urls <-
c("https://www.ratemyprofessors.com/professor/2043993",
"https://www.ratemyprofessors.com/professor/201983",
"https://www.ratemyprofessors.com/professor/961562",
"https://www.ratemyprofessors.com/professor/2235761",
"https://www.ratemyprofessors.com/professor/1280719",
"https://www.ratemyprofessors.com/professor/577226",
"https://www.ratemyprofessors.com/professor/1125627",
"https://www.ratemyprofessors.com/professor/795918",
"https://www.ratemyprofessors.com/professor/2069848",
"https://www.ratemyprofessors.com/professor/2765715",
"https://www.ratemyprofessors.com/professor/2131554",
"https://www.ratemyprofessors.com/professor/652638",
"https://www.ratemyprofessors.com/professor/2254361",
"https://www.ratemyprofessors.com/professor/182004",
"https://www.ratemyprofessors.com/professor/1653896",
"https://www.ratemyprofessors.com/professor/374369",
"https://www.ratemyprofessors.com/professor/1408151",
"https://www.ratemyprofessors.com/professor/173802",
"https://www.ratemyprofessors.com/professor/797569")
professor_data <- data.frame()
for (i in urls) {
professor_data <-
professor_data %>%
rbind(rmp_scrape_page(i))
Sys.sleep(3)
}
#### Relationship b/n course difficulty and rating
professor_data %>%
ggplot(aes(x = course_difficulty, y = rating))+
geom_point()+
labs(title = "Relationship b/n course difficulty and rating",
x = "Course Difficulty",
y = "Rating")

# I can conclude that there is no relationship between course difficulty and
# rating because there are no clusters from the data.
#### Visualizing which professor has the highest average professor rating ####
# Average Rating by Professor
professor_data %>%
group_by(professor) %>%
summarise(`AVG Rating per Professor` = mean(rating)) %>%
ggplot(aes(x = professor, y = `AVG Rating per Professor`))+
geom_col()+
labs(title = "Average Rating by Professor",
x = "Professor",
y = "Average Rating")

# From this visual we can conclude that Greg Smith has the highest average
# rating amongst Joel Asay and Mark Sena. However, Joel has one outlier where
# he was rated a 1 because he is "addicted to discord". Excluding this review
# leave his average at a perfect 5.
#### Ratings over time ####
professor_data %>%
ggplot(aes(x = date, y = rating))+
geom_line()+
labs(title = "Ratings over time",
x = "Date",
y = "Rating")

# This visual shows the wide variety in ratings over the years. Reviews in 2025
# tend to see more varianc
# Joel "addicted to discord" review
professor_data %>%
filter(professor == "Joel Asay") %>%
filter(rating == 1) %>% view()
write.csv(professor_data, file = "professor_data.csv", row.names = FALSE)