Overview

This report scrapes basic SEO elements from Walmart and Target.

Load Libraries

library(rvest)
library(stringr)
library(dplyr)

Define URLs

urls <- c(
  "https://www.walmart.com",
  "https://www.target.com"
)

Scrape SEO Data

results <- list()

for (i in seq_along(urls)) {

  Sys.sleep(1.5)

  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()

  h1_tags <- page %>%
    html_elements("h1") %>%
    html_text2() %>%
    str_squish() %>%
    str_c(collapse = " | ")

  h2_tags <- page %>%
    html_elements("h2") %>%
    html_text2() %>%
    str_squish() %>%
    str_c(collapse = " | ")

  h3_tags <- page %>%
    html_elements("h3") %>%
    html_text2() %>%
    str_squish() %>%
    str_c(collapse = " | ")

  results[[i]] <- tibble(
    url = urls[i],
    title = title_tag,
    meta_description = meta_desc,
    h1 = h1_tags,
    h2 = h2_tags,
    h3 = h3_tags
  )
}

Combine Results

seo_data <- bind_rows(results)

seo_data
## # A tibble: 2 × 6
##   url                     title               meta_description h1    h2    h3   
##   <chr>                   <chr>               <chr>            <chr> <chr> <chr>
## 1 https://www.walmart.com Walmart | Save Mon… Shop Walmart.co… Walm… Disc… Brio…
## 2 https://www.target.com  Target : Expect Mo… Shop Target onl… Home… Endl… Load…

Export Results

write.csv(seo_data, file = "walmart_target_seo_data.csv", row.names = FALSE)

cat("SEO data exported to 'walmart_target_seo_data.csv'")
## SEO data exported to 'walmart_target_seo_data.csv'