Easily Harvest (Scrape) Web Pages

Wrappers around the 'xml2' and 'httr' packages to make it easy to download, then manipulate, HTML and XML.


rvest rvest website

CRANstatus R-CMD-check Codecov testcoverage

Overview

rvest helps you scrape (or harvest) data from web pages. It is designed to work with magrittr to make it easy to express common web scraping tasks, inspired by libraries like beautiful soup and RoboBrowser.

If you’re scraping multiple pages, I highly recommend using rvest in concert with polite. The polite package ensures that you’re respecting the robots.txt and not hammering the site with too many requests.

Installation

# The easiest way to get rvest is to install the whole tidyverse:
install.packages("tidyverse")

# Alternatively, install just rvest:
install.packages("rvest")

Usage

library(rvest)

# Start by reading a HTML page with read_html():
starwars <- read_html("https://rvest.tidyverse.org/articles/starwars.html")

# Then find elements that match a css selector or XPath expression
# using html_elements(). In this example, each <section> corresponds
# to a different film
films <- starwars |> html_elements("section")
films
#> {xml_nodeset (7)}
#> [1] <section><h2 data-id="1">\nThe Phantom Menace\n</h2>\n<p>\nReleased: 1999 ...
#> [2] <section><h2 data-id="2">\nAttack of the Clones\n</h2>\n<p>\nReleased: 20 ...
#> [3] <section><h2 data-id="3">\nRevenge of the Sith\n</h2>\n<p>\nReleased: 200 ...
#> [4] <section><h2 data-id="4">\nA New Hope\n</h2>\n<p>\nReleased: 1977-05-25\n ...
#> [5] <section><h2 data-id="5">\nThe Empire Strikes Back\n</h2>\n<p>\nReleased: ...
#> [6] <section><h2 data-id="6">\nReturn of the Jedi\n</h2>\n<p>\nReleased: 1983 ...
#> [7] <section><h2 data-id="7">\nThe Force Awakens\n</h2>\n<p>\nReleased: 2015- ...

# Then use html_element() to extract one element per film. Here
# we the title is given by the text inside <h2>
title <- films |> 
  html_element("h2") |> 
  html_text2()
title
#> [1] "The Phantom Menace"      "Attack of the Clones"   
#> [3] "Revenge of the Sith"     "A New Hope"             
#> [5] "The Empire Strikes Back" "Return of the Jedi"     
#> [7] "The Force Awakens"

# Or use html_attr() to get data out of attributes. html_attr() always
# returns a string so we convert it to an integer using a readr function
episode <- films |> 
  html_element("h2") |> 
  html_attr("data-id") |> 
  readr::parse_integer()
episode
#> [1] 1 2 3 4 5 6 7

If the page contains tabular data you can convert it directly to a data frame with html_table():

html <- read_html("https://en.wikipedia.org/w/index.php?title=The_Lego_Movie&oldid=998422565")

html |> 
  html_element(".tracklist") |> 
  html_table()
#> # A tibble: 29 × 4
#>    No.   Title                       `Performer(s)`                       Length
#>    <chr> <chr>                       <chr>                                <chr> 
#>  1 1.    "\"Everything Is Awesome\"" "Tegan and Sara featuring The Lonel… 2:43  
#>  2 2.    "\"Prologue\""              ""                                   2:28  
#>  3 3.    "\"Emmett's Morning\""      ""                                   2:00  
#>  4 4.    "\"Emmett Falls in Love\""  ""                                   1:11  
#>  5 5.    "\"Escape\""                ""                                   3:26  
#>  6 6.    "\"Into the Old West\""     ""                                   1:00  
#>  7 7.    "\"Wyldstyle Explains\""    ""                                   1:21  
#>  8 8.    "\"Emmett's Mind\""         ""                                   2:17  
#>  9 9.    "\"The Transformation\""    ""                                   1:46  
#> 10 10.   "\"Saloons and Wagons\""    ""                                   3:38  
#> # ℹ 19 more rows

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("rvest")

1.0.5 by Hadley Wickham, a year ago


https://rvest.tidyverse.org/, https://github.com/tidyverse/rvest


Report a bug at https://github.com/tidyverse/rvest/issues


Browse source code at https://github.com/cran/rvest


Authors: Hadley Wickham [aut, cre] , Posit Software , PBC [cph, fnd] (ROR: <https://ror.org/03wc8by49>)


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports cli, glue, httr, lifecycle, magrittr, rlang, selectr, tibble, xml2

Suggests chromote, covr, knitr, purrr, R6, readr, repurrrsive, rmarkdown, spelling, stringi, testthat, tidyr, webfakes


Imported by BAwiR, BFS, BIS, BLSloadR, BOJ, ECOTOXr, Ecfun, FMAT, GEInfo, GetDFPData2, GetFREData, GetTDData, HLAtools, HMDHFDplus, IRexamples, IceSat2R, MDPIexploreR, MazamaCoreUtils, MediaNews, MortalityLaws, N2H4, NHSDataDictionaRy, OpenSourceAP.DownloadR, OralOpioids, RAGFlowChainR, RBNZ, RivRetrieve, Rsagacmd, SMMT, SanFranBeachWater, SchoolDataIT, SubtypeDrug, SwimmeR, Tivy, WaterBalanceR, addinslist, adjustedcranlogs, aebdata, aire.zmvm, archiveRetriever, banxicoR, baseballr, basifoR, bolsec, bursa, c2z, canpumf, cdlTools, chessResults, clidatajp, cms, coalitions, crawlee, cricketdata, ctrdata, datamedios, datasus, denguedatahub, dtlscores, easyRef, easyScieloPack, effectcheck, emayili, epidata, essurvey, etl, etrader, eurlex, fastRhockey, fastqcr, fitzRoy, flightsbr, foghorn, foqat, frenchdata, gcite, geneNR, geniusr, geoidep, gfer, glossary, gptstudio, gtranslate, hockeystick, hoopR, html2excel, icpsrdata, ifo, ihpdr, indonesiaFootballScoutR, izmir, jmastats, jstager, juicedown, lares, letsHerp, letsRept, lobsteR, lovecraftr, mRpostman, macleish, margaret, matchpointR, maths.genealogy, mergenstudio, meteospain, metricminer, modisfast, monashtipr, moranajp, ncaavolleyballr, nhanesA, nhdR, nrlR, nser, oddsapiR, opencis, opengraph, opinAr, osmdata, ottrpal, owidR, partycoloR, pda, pedquant, peeky, photon, pkgcheck, pkgdiff, pkgmatch, polyglotr, postlightmercury, proverbs, provolleyballr, quincunx, rStrava, ragnar, ralger, rcatfish, readabs, readapra, readrba, ready4, realestatebr, redlist, relSim, reptiledb.data, retrosheet, rfair, rgenius, rgoogleads, ropenblas, rspacer, rstudio.prefs, rtrek, scholar, scraEP, scrapeR, scrappy, sejmRP, senatebR, sjtable2df, snotelr, sorvi, speaq, speech, speechbr, spotifyr, stortingscrape, surveydown, taxodist, teal.widgets, textpress, tidycensus, tidytuesdayR, tidyverse, tinyscholar, tm.plugin.factiva, toolero, traits, traktok, trud, tutorial.helpers, uscongress, usedthese, videogameinsightsR, webchem, wehoop, wikifacts, wikilake, worldfootballR, yfR, ztype.

Depended on by BatchGetSymbols, FFdownload.

Suggested by AMR, BeeBDC, CRediTas, DOPE, ICD10gm, LLMAgentR, NBAloveR, PortfolioTesteR, RSQLite, RaMS, SingleCaseES, arcpullr, bdc, clinify, convergEU, dataquieR, ecochange, fImport, fuzzyjoin, fuzzystring, gasper, gie, gluedown, gofigR, googleLanguageR, gt, gtExtras, hydrodownloadR, ibawds, ie2miscdata, k5, mapme.biodiversity, mpathsenser, mscstts, naaccr, nascaR.data, newsanchor, nhanesR, nocturn, r4ds.tutorials, rco, readMDTable, reproducible, restatis, rex, roperators, rscopus, runexp, rworkflows, scdhlm, selenider, sharpshootR, soilDB, teal, teal.modules.clinical, teal.modules.general, teal.picks, teal.slice, units, unpivotr, volcanoes, vosonSML, worrrd.


See at CRAN