Scraping URL’s

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://predatorcues.com/?srsltid=AfmBOooCObu844gFMqiJ0c_YJl3wRnnzKwIr_Y2JoBz-Vf1CpLxHf-4S",
  "https://www.brunswickbilliards.com/?srsltid=AfmBOor4GvLTsaqoATKZXi_72ZgHbEt16bBx-N3uq893lMXpZozq64MT", 
"https://www.mcdermottcue.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()
  
  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://predatorcues.com/?srsltid=Af… Pred… Our Team of Cha… "HIG… "Red… "Car…
## 2 https://www.brunswickbilliards.com/?… Bill… For 180 years, … ""    "Ite… "All…
## 3 https://www.mcdermottcue.com/         McDe… Maker of handcr… "Fre… ""    ""
write.csv(seo_data, file = "seo_data.csv", row.names = FALSE)

seo_data$meta_description
## [1] "Our Team of Champions trusts their game to Predator thanks to Predator's low-deflection shafts and performance-enhancing technology. Shop professional pool cues, pool tables, low-deflection shafts, and billiard equipment."
## [2] "For 180 years, Brunswick Billiards has designed and crafted quality gaming tables for homes and businesses. Find your local Brunswick Billiards dealer today."                                                                
## [3] "Maker of handcrafted billiard cues that blend groundbreaking technology with quality materials and exquisite artistry. Started by a cue-making legend in 1975."
seo_data$h1
## [1] "HIGH-PERFORMANCE POOL CUES, TABLES, AND BILLIARD ACCESSORIES"
## [2] ""                                                            
## [3] "Free Pool Cue Giveaway"
seo_data$h2
## [1] "Red, White & Cue | Break With Power | Jump With Power | Cool. Calculated. | New Arrivals | #1 World's Best Pool Cues and Billiard Equipment | SP2 Palisander Pool Cue | SP2 Lime Green Pool Cue | Roadline Teal Pool Case | Artisan Pool Cue | Blak5 Pool Cue | 10K Pool Cue | True Splice 16 Pool Cue | Maelith POOL CUE | CANDY POOL CUE | BK RUSH+ Jump/Break Cue | Being first has its perks"
## [2] "Item added to your cart | Over 180 Years of Innovation | Over 180 Years of Innovation | Industry-Leading Best Sellers | Rustic Charm Meets Modern Industrial Design Style | Featured Articles | Customer Care | Customer Care | Company | Company | Shop | Shop | Stay in Touch"                                                                                                                 
## [3] ""
seo_data$h3
## [1] "Carbon Fiber Shaft Cues | Pool Tables | Best Pool Cue Brands | Top Pool Cue Picks | Why do players choose Predator?"
## [2] "Allenton 7' Pool Table with Tapered Leg | Glenwood 7' Pool Table with Ball & Claw Leg | Centennial 8' Pool Table"   
## [3] ""

Findings

I believe out of the three Billiards brands, Predator cues showed the strongest in SEO. I do think that their meta description as well as the title tags were more detailed. I see that they added more product details and descriptions in their titles while the others didn’t have as much.