Scraping Tables and Links
Parse HTML tables into data frames and collect all hyperlinks on a page.
HTML Tables in Web Pages
HTML tables (<table>) are the most convenient targets for scraping because they already have structure. rvest's html_table() converts them directly into R data frames, handling headers automatically.
library(rvest)
html <- read_html('
<table>
<thead><tr><th>Country</th><th>GDP</th><th>Pop</th></tr></thead>
<tbody>
<tr><td>USA</td><td>25T</td><td>330M</td></tr>
<tr><td>China</td><td>18T</td><td>1400M</td></tr>
</tbody>
</table>
')
# html_table converts to data frame
tbl <- html_table(html_element(html, 'table'))
class(tbl) # 'data.frame'
names(tbl) # c('Country', 'GDP', 'Pop')
nrow(tbl) # 2
print(tbl)html_table() on a Full Page
When a page has multiple tables, use html_elements() (plural) to get all tables, then html_table() on the resulting nodeset to return a list of data frames. Select the one you need by index.
library(rvest)
html <- read_html('
<table id="t1"><tr><th>A</th></tr><tr><td>1</td></tr></table>
<table id="t2"><tr><th>X</th><th>Y</th></tr>
<tr><td>10</td><td>20</td></tr></table>
')
# Get ALL tables as a list of data frames
tables <- html_table(html_elements(html, 'table'))
length(tables) # 2
# First table
tables[[1]]
# Second table
tables[[2]]
# Select a specific table by its id
table2 <- html_table(html_element(html, '#t2'))
print(table2)