I'm trying to scrape reviews from Glassdoor using Rvest and place the results in a data frame with one row per review. My code is below, but the section where I try to pull the sub-ratings (work-life balance, culture and values, etc) doesn't work. There are five different sub-ratings within a drop down, and one or more of them may be blank for each review. Here's my preliminary code. Do you have any suggestions for how I can pull the sub-ratings and put each sub-rating in a separate column in my data frame?
## Load libraries
library(httr)
library(xml2)
library(rvest)
library(purrr)
library(tidyverse)
library(lubridate)
## URL for scraping
url = "https://www.glassdoor.com/Reviews/Google-Reviews-E9079.htm"
pg_reviews = read_html(url)
##Create data frame and define features to scrape
Google_reviews = data.frame()
class.ratings = c()
styles = pg_reviews %>% html_elements('style')
for(s in styles) {
class = s %>% html_attr('data-emotion-css')
class = paste0('css-', class)
rating = str_match(s %>% html_text2(), '(\\d+)%')[2]
class.ratings[class] = as.numeric(rating)/20
}
reviews = pg_reviews %>% html_elements('.gdReview')
summary = pg_reviews %>%
html_elements(".reviewLink") %>%
html_text()
rating = pg_reviews %>%
html_elements("#ReviewsFeed .mr-xsm") %>%
html_text()
pros = pg_reviews %>%
html_elements(".v2__EIReviewDetailsV2__fullWidth:nth-child(1) span") %>%
html_text()
cons = pg_reviews %>%
html_elements(".v2__EIReviewDetailsV2__fullWidth:nth-child(2) span") %>%
html_text()
#Subratings--DOES NOT WORK
for(re in reviews) {
subratings = re %>% html_elements('.content') %>% html_elements('li')
for(i = 1 to 5) {
label = i %>% html_element('div') %>% html_text()
classes = i %>% html_elements('div[font-size="sm"]') %>% html_attr('class')
class = str_split(classes, ' ')[[1]][1] # take the first class attribute
cat(class.ratings[class], ',')
}
work_life_balance <- subratings(1)
culture_values <- subratings(2)
career_opportunities <- subratings(3)
comp_benefits <- subratings(4)
management <- subratings(5)
}
Google_reviews = rbind(Google_reviews,data.frame(summary,rating,pros,cons,work_life_balance,culture_values
career_opportunities,comp_benefits,management))
'''