Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions R/get_daily_horoscope.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,32 @@ getdailyhoroscope <- function(zodiacsign) {
# helper function to get only one horoscope by webscrapping horoscope.com

get_onedaily_horoscope <- function(page_number){
Sys.sleep(1)

url <- paste0("https://www.horoscope.com/us/horoscopes/general/horoscope-general-daily-today.aspx?sign=", page_number)
sign_link <- rvest::read_html(url) # reading the url

sign <- sign_link %>% # getting the sign from the webpage
rvest::html_node("h1") %>%
rvest::html_text(trim = TRUE)
sign <- stringr::str_replace_all(sign, "Horoscope", "") #trimming excess words
sign <- tolower(sign)
rvest::html_text(trim = TRUE) %>%
stringr::str_replace_all("Horoscope", "") %>% #trimming excess words
tolower() %>%
stringr::str_trim()


daily_horoscope <- sign_link %>% # reading in the daily horoscope
rvest::html_node(".switcher+ p") %>%
rvest::html_text(trim = TRUE)

horoscope_info <- data.frame(sign, daily_horoscope[1]) #putting into dataframe
horoscope_info <- data.frame(sign, daily_horoscope) #putting into dataframe
names(horoscope_info) <- c("sign","horoscope")
return(horoscope_info) # return that sign
}


# Building data frame with all 12 zodiac signs and their respective daily horoscopes

horoscope_data <- data.frame(sign = character(),
horoscope = character(),
stringsAsFactors = FALSE) #creating empty dataframe
for (sign in 1:12){
horoscope_infor <- get_onedaily_horoscope(sign) #looping thru and getting each of 12 signs
# Making data frame with the 12 zodiac signs and their respective daily horoscopes
sign_numbers <- 1:12
horoscope_list <- lapply(sign_numbers, get_onedaily_horoscope) #using lapply for vectorization
horoscope_data <- bind_rows(horoscope_list) #combining all the data frames into one

horoscope_infor <- as.data.frame(lapply(horoscope_infor, stringr::str_trim),
stringsAsFactors = FALSE) # trimming whitespace

horoscope_data <- rbind(horoscope_data, horoscope_infor) #adding sign to dataframe

Sys.sleep(1) #adding a one second pause to not overload server
}
print(horoscope_data) #viewing final dataframe