install.packages(c(“rvest”, “stringr”, “httr2”, “dplyr”))
# Library Load
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
request("https://www.mazdausa.com/") %>%
req_user_agent("Mozilla/5.0") %>%
req_perform()
## <httr2_response>
## GET https://www.mazdausa.com/
## Status: 200 OK
## Content-Type: text/html
## Body: In memory (574417 bytes)
request("https://www.toyota.com/") %>%
req_user_agent("Mozilla/5.0") %>%
req_perform()
## <httr2_response>
## GET https://www.toyota.com/
## Status: 200 OK
## Content-Type: text/html
## Body: In memory (652355 bytes)
request("https://www.nissanusa.com/") %>%
req_user_agent("Mozilla/5.0") %>%
req_perform()
## <httr2_response>
## GET https://www.nissanusa.com/
## Status: 200 OK
## Content-Type: text/html
## Body: In memory (610380 bytes)
urls <- c(
Mazda = "https://www.mazdausa.com/",
Toyota = "https://global.toyota/en/",
Nissan = "https://www.nissan-global.com/EN/"
)
results <- list()
for (i in seq_along(urls)) {
Sys.sleep(1.5)
response <- request(urls[i]) %>%
req_user_agent("Mozilla/5.0") %>%
req_perform()
page <- response %>%
resp_body_html()
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 = " | ")
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(
company = names(urls)[i],
url = urls[i],
title = title_tag,
meta_description = meta_desc,
h1 = h1_tags,
h2 = h2_tags,
h3 = h3_tags
)
}
seo_data <- bind_rows(results)
seo_data
## # A tibble: 3 × 7
## company url title meta_description h1 h2 h3
## <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 Mazda https://www.mazdausa.com/ Mazd… The official Ma… "THE… "ELE… "You…
## 2 Toyota https://global.toyota/en/ Toyo… Toyota Motor Co… "" "Toy… ""
## 3 Nissan https://www.nissan-global.co… Niss… Nissan Motor Co… "" "Our… ""
write.csv(seo_data, file = "seo_data.csv", row.names = FALSE)
seo_data$title
## [1] "Mazda USA Official Site | Cars, SUVs & Crossovers | Mazda USA"
## [2] "Toyota Motor Corporation Official Global Website"
## [3] "Nissan Motor Corporation Global Website"
seo_data$meta_description
## [1] "The official Mazda site to research and shop for all Mazda vehicles. Explore our models, features, photos, specs, build your own, and more on MazdaUSA.com."
## [2] "Toyota Motor Corporation Official Global Website―company, ir, newsroom, mobility, sustainability."
## [3] "Nissan Motor Corporation Global Website: Visit the site for information about Nissan, sustainability, IR, and innovation. This site also provides various Nissan initiatives, including design, safety, quality, and community engagement."
seo_data$h1
## [1] "THE MORE TO MOVE YOU SALES EVENT | THE MORE TO MOVE YOU SALES EVENT | "
## [2] ""
## [3] ""
seo_data$h2
## [1] "ELECTRIFIED OPTIONS TO FIT YOUR LIFE | EXPLORE | Shop | Buy | Maintaining Your Mazda | Making it Yours | ELECTRIFIED OPTIONS TO FIT YOUR LIFE | EXPLORE | Shop | Buy | Maintaining Your Mazda | Making it Yours | Explore Mazda | A MAZDA MX-5 OWNER 25 YEARS IN THE MAKING | MIATA FANATIC GETS FIRST MX-5 MIATA RF | MAZDA AND NATIONAL GEOGRAPHIC’S PHOTOGRAPHY QUEST | MEET THE MAN BEHIND MAZDA’S HERITAGE COLLECTION | Explore Mazda | A MAZDA MX-5 OWNER 25 YEARS IN THE MAKING | MIATA FANATIC GETS FIRST MX-5 MIATA RF | MAZDA AND NATIONAL GEOGRAPHIC’S PHOTOGRAPHY QUEST | MEET THE MAN BEHIND MAZDA’S HERITAGE COLLECTION | | DRIVING FORWARD WITH CONFIDENCE | COMMITTED TO SAFETY | GET STARTED WITH SAVINGS | EMBRACING THE ROAD AHEAD | MORE POWERTRAINS, MORE OPTIONS. | | FIND INSPIRATION AT YOUR LOCAL DEALER | ELEVATE YOUR OWNERSHIP EXPERIENCE | MAZDA OWNERS LOGIN | SCHEDULE SERVICE"
## [2] "Toyota Launches New Hilux in Japan | Toyota Launches All-New Land Cruiser \"FJ\" Series in Japan | TMC's FY2026 Financial Results Press Briefing | Latest News | Our Picks | Latest Updates | Notices"
## [3] "Our Company | Investors | Sustainability | Nissan Stories | Global | News Releases | Join our team | Follow Us | Related Website"
seo_data$h3
## [1] "Your Location | | | | | SPECIAL OFFERS | | | | | THERE'S A NEW LEADER IN SAFETY | MAZDA OWNERS LOGIN | SCHEDULE SERVICE"
## [2] ""
## [3] ""
I compared 3 car brands: Mazda, Toyota, and Nissan. Nissan had the cleanest SEO results, while Mazda had the messiest (keyword rich). Toyota came in second place with relatively few heading tags, which suggests that some content is generated dynamically. Overall, because its title, meta description, and heading hierarchy were concise, descriptive, and well organized, Nissan remained the top choice.