scraping urls for quick SEO inisghts

library(rvest)
library(stringr)
library(httr2)
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
# List of URLs to scrape
urls <- c(
  "https://www.llbean.com",
  "https://www.patagonia.com", 
  "https://www.nike.com"
)

# Empty list to collect results
results <- list()

for (i in seq_along(urls)) {
  
  Sys.sleep(1.5) # polite delay between requests
  
  page <- read_html(urls[i])
  
  title_tag <- page %>%
    html_element("title") %>%
    html_text2() %>%
    str_squish()
 title_tag 
  meta_desc <- page %>%
    html_element("meta[name='description']") %>%
    html_attr("content") %>%
    str_squish()
 meta_desc
 
  h1_tags <- page %>%
    html_elements("h1") %>%
    html_text2() %>%
    str_squish() %>%
    str_c(collapse = " | ")
h1_tags
  
  h2_tags <- page %>%
    html_elements("h2") %>%
    html_text2() %>%
    str_squish() %>%
    str_c(collapse = " | ")

  h2_tags
    
  h3_tags <- page %>%
    html_elements("h3") %>%
    html_text2() %>%
    str_squish() %>%
    str_c(collapse = " | ")
  
  h3_tags
  
  # Store this page's results as a one-row tibble
  results[[i]] <- tibble(
    url = urls[i],
    title = title_tag,
    meta_description = meta_desc,
    h1 = h1_tags,
    h2 = h2_tags,
    h3 = h3_tags
  )
}

# Combine all rows into one data frame
seo_data <- bind_rows(results)

seo_data
## # A tibble: 3 × 6
##   url                       title             meta_description h1    h2    h3   
##   <chr>                     <chr>             <chr>            <chr> <chr> <chr>
## 1 https://www.llbean.com    L.L.Bean - Outsi… Enjoy Free Ship… "L.L… "It'… ""   
## 2 https://www.patagonia.com Hang Tight! Rout… <NA>             ""    "Sit… "U.S…
## 3 https://www.nike.com      Nike. Just Do It… Inspiring the w… ""    "Run… "ROW…
write.csv(seo_data, file = "seo_data.csv", row.names = FALSE)

seo_data$title
## [1] "L.L.Bean - Outside Together Since 1912"
## [2] "Hang Tight! Routing to checkout..."    
## [3] "Nike. Just Do It. Nike.com"
seo_data$meta_description
## [1] "Enjoy Free Shipping with $75 purchase on clothing, shoes, outdoor gear and more, all made for the shared joy of the outdoors."
## [2] NA                                                                                                                             
## [3] "Inspiring the world's athletes, Nike delivers innovative products, experiences and services."
seo_data$h1
## [1] "L.L.Bean - Outside Together Since 1912. Free Shipping with $75 purchase."
## [2] ""                                                                        
## [3] ""
seo_data$h2
## [1] "It's time to upgrade your browser | Best Worn to Make Summer Memories | Best Worn to Make Summer Memories"                                                                                                                                            
## [2] "Sit tight"                                                                                                                                                                                                                                            
## [3] "Run Free, Play Forever | Nike Vapor Edge 360 \"Untouchable\" | Run Free, Play Forever | Nike Vapor Edge 360 \"Untouchable\" | Lock In. Level Up. | Lock In. Level Up. | TRENDING | TRENDING | Africa | Americas | Asia Pacific | Europe | Middle East"
seo_data$h3
## [1] ""                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
## [2] "U.S. & CANADA | EUROPE | JAPAN"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
## [3] "ROWING STRONG | HIS LEGEND LIVES ON | IRRATIONAL BELIEF | THREE LION HEARTED | ROWING STRONG | HIS LEGEND LIVES ON | SWEAT. PROOF. | SWEAT. PROOF. | ACG Radical AirFlow | Play outside the lines with Nike Free Ride’s flexible innovation, now in fresh new colors. | The cleat that took the world by storm, modernized with our greatest innovations. | Nothing sweet About it | A Striking Approach | One on Anyone | Play outside the lines with Nike Free Ride’s flexible innovation, now in fresh new colors. | The cleat that took the world by storm, modernized with our greatest innovations. | Nothing sweet About it | A Striking Approach | One on Anyone | Practice Ready | First Day Footwear | Classroom Ready | Bring serious style to their everyday game. | FULL HORSEPOWER | FULL HORSEPOWER"

Conclusion

THis is a very important to see how you compare in the market place based of your competitors. Decesion makers could use data like this to make informed decesions on campagins and marketing straegies