Scrape a table from a website in R with rvest
In R, rvest::read_html(url) |> html_elements("table") |> html_table() returns a list of tibbles, one per table. Pick the one you need and clean names with janitor. If the table needs a login or JavaScript, export it as CSV from the browser and read it with readr::read_csv.
Do this in one click with LightCube Data Extractor, a free Chrome extension. No account needed.
Add to Chrome, free ↗Basic rvest example
library(rvest)
page <- read_html("https://example.com/page")
tables <- page |> html_elements("table") |> html_table()
df <- tables[[1]] |> janitor::clean_names()
readr::write_csv(df, "table.csv")
Choosing the right table
Use a CSS selector to target it directly, for example html_element("table.wikitable"). The selector guide explains how to find one.
From browser CSV to R
df <- readr::read_csv("table.csv", col_types = readr::cols(.default = "c"))
Reading every column as character first protects leading zeros and odd dates; convert types afterwards.
Be polite
Add Sys.sleep(1) between requests in loops and check the site terms.
FAQ
- Does rvest run JavaScript?
- No. Use a headless browser or export from Chrome.
- How do I get links instead?
html_elements("a") |> html_attr("href").- Is a browser export reproducible?
- Less so; save the CSV and note the date and URL.
Related guides
Updated 2026-11-11.