library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidytext)
library(widyr)
library(igraph)
##
## Attaching package: 'igraph'
##
## The following objects are masked from 'package:lubridate':
##
## %--%, union
##
## The following objects are masked from 'package:dplyr':
##
## as_data_frame, groups, union
##
## The following objects are masked from 'package:purrr':
##
## compose, simplify
##
## The following object is masked from 'package:tidyr':
##
## crossing
##
## The following object is masked from 'package:tibble':
##
## as_data_frame
##
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
##
## The following object is masked from 'package:base':
##
## union
library(ggraph)
library(kableExtra)
##
## Attaching package: 'kableExtra'
##
## The following object is masked from 'package:dplyr':
##
## group_rows
library(rvest)
##
## Attaching package: 'rvest'
##
## The following object is masked from 'package:readr':
##
## guess_encoding
library(httr2)
library(xml2)
##
## Attaching package: 'xml2'
##
## The following object is masked from 'package:httr2':
##
## url_parse
library(stringr)
# List of URLs to scrape
urls <- c(
"https://www.subaru.com",
"https://www.toyota.com",
"https://www.mazdausa.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.subaru.com Subaru Cars, SUVs… Explore Subaru'… "" "" ""
## 2 https://www.toyota.com New Cars, Trucks,… Explore the new… "" "202… "pic…
## 3 https://www.mazdausa.com Mazda USA Officia… The official Ma… "THE… "ELE… "You…
write.csv(seo_data, file = "seo_data.csv", row.names = FALSE)
seo_data$title
## [1] "Subaru Cars, SUVs, Crossovers & Hybrids | Subaru of America"
## [2] "New Cars, Trucks, SUVs & Hybrids | Toyota Official Site"
## [3] "Mazda USA Official Site | Cars, SUVs & Crossovers | Mazda USA"
seo_data$meta_description
## [1] "Explore Subaru's exceptional cars, crossovers & SUVs including Outback, Ascent and Crosstrek. View photos, specs, compare models, and build & price your own."
## [2] "Explore the newest Toyota trucks, cars, SUVs, hybrids and minivans. See photos, compare models, get tips, calculate payments, and more."
## [3] "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."
seo_data$h1
## [1] ""
## [2] ""
## [3] "THE MORE TO MOVE YOU SALES EVENT | THE MORE TO MOVE YOU SALES EVENT | "
seo_data$h2
## [1] ""
## [2] "2027 Prius | 2027 Prius Plug-in Hybrid | 2026 GRMN Corolla | 2027 Land Cruiser | 2026 Camry | Explore All Vehicles | Shopping Tools | Discover Toyota | SHOPPING TOOLS | VEHICLES | HELPFUL LINKS | OWNERS | ABOUT TOYOTA"
## [3] "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"
seo_data$h3
## [1] ""
## [2] "pick up where you left off | pick up where you left off | A lineup generating more possibilities. | External Site Modal"
## [3] "Your Location | | | | | SPECIAL OFFERS | | | | | THERE'S A NEW LEADER IN SAFETY | MAZDA OWNERS LOGIN | SCHEDULE SERVICE"
Mazada has the best SEO it conatins alot of usefule tags that are able to be picked by a search engine and added. Subaru fails to show some data when scraping data toyota fails to show some tags but it is better than subaru.