Easily Harvest (Scrape) Web Pages

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

CRANstatus Travis buildstatus Codecov testcoverage


rvest helps you scrape information 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.

lego_movie <- read_html("http://www.imdb.com/title/tt1490017/")
rating <- lego_movie %>% 
  html_nodes("strong span") %>%
  html_text() %>%
#> [1] 7.8
cast <- lego_movie %>%
  html_nodes("#titleCast .primary_photo img") %>%
#>  [1] "Will Arnett"     "Elizabeth Banks" "Craig Berry"    
#>  [4] "Alison Brie"     "David Burrows"   "Anthony Daniels"
#>  [7] "Charlie Day"     "Amanda Farinos"  "Keith Ferguson" 
#> [10] "Will Ferrell"    "Will Forte"      "Dave Franco"    
#> [13] "Morgan Freeman"  "Todd Hansen"     "Jonah Hill"
poster <- lego_movie %>%
  html_nodes(".poster img") %>%
#> [1] "https://m.media-amazon.com/images/M/[email protected]_V1_UX182_CR0,0,182,268_AL_.jpg"


Install the release version from CRAN:


Or the development version from GitHub

# install.packages("devtools")

Key functions

The most important functions in rvest are:

  • Create an html document from a url, a file on disk or a string containing html with read_html().

  • Select parts of a document using CSS selectors: html_nodes(doc, "table td") (or if you’ve a glutton for punishment, use XPath selectors with html_nodes(doc, xpath = "//table//td")). If you haven’t heard of selectorgadget, make sure to read vignette("selectorgadget") to learn about it.

  • Extract components with html_name() (the name of the tag), html_text() (all text inside the tag), html_attr() (contents of a single attribute) and html_attrs() (all attributes).

  • (You can also use rvest with XML files: parse with xml(), then extract components using xml_node(), xml_attr(), xml_attrs(), xml_text() and xml_name().)

  • Parse tables into data frames with html_table().

  • Extract, modify and submit forms with html_form(), set_values() and submit_form().

  • Detect and repair encoding problems with guess_encoding() and repair_encoding().

  • Navigate around a website as if you’re in a browser with html_session(), jump_to(), follow_link(), back(), forward(), submit_form() and so on. (This is still a work in progress, so I’d love your feedback.)

To see examples of these function in use, check out the demos.



rvest 0.3.3

  • Fix R CMD check failure

  • submit_request() now checks for empty form-field-types to select the correct submit fields (@rentrop, #159)

rvest 0.3.2

  • Fixes to follow_link() and back() to correctly manage session history.

  • If you're using xml2 1.0.0, html_node() will now return a "missing node".

  • Parse rowspans and colspans effectively by filling using repetition from left to right (for colspan) and top to bottom (rowspan) (#111)

  • Updated a few examples and demos where the website structure has changed.

  • Made compatible with both xml2 0.1.2 and 1.0.0.

rvest 0.3.1

  • Fix invalid link for SSA example.

  • Parse <options> that don't have value attribute (#85).

  • Remove all remaining uses of html() in favor of read_html() (@jimhester, #113).

rvest 0.3.0

  • rvest has been rewritten to take advantage of the new xml2 package. xml2 provides a fresh binding to libxml2, avoiding many of the work-arounds previously needed for the XML package. Now rvest depends on the xml2 package, so all the xml functions are available, and rvest adds a thin wrapper for html.

  • A number of functions have change names. The old versions still work, but are deprecated and will be removed in rvest 0.4.0.

    • html_tag() -> html_name()
    • html() -> read_html()
  • html_node() now throws an error if there are no matches, and a warning if there's more than one match. I think this should make it more likely to fail clearly when the structure of the page changes.

  • xml_structure() has been moved to xml2. New html_structure() (also in xml2) highlights id and class attributes (#78).

  • submit_form() now works with forms that use GET (#66).

  • submit_request() (and hence submit_form()) is now case-insensitive, and so will find <input type=SUBMIT> as well as<input type="submit">.

  • submit_request() (and hence submit_form()) recognizes forms with <input type="image"> as a valid form submission button.

rvest 0.2.0

New features

  • html() and xml() pass ... on to httr::GET() so you can more finely control the request (#48).

  • Add xml support: parse with xml(), then work with using xml_node(), xml_attr(), xml_attrs(), xml_text() and xml_tag() (#24).

  • xml_structure(): new function that displays the structure (i.e. tag and attribute names) of a xml/html object (#10).

Bug fixes

  • follow_link() now accepts css and xpath selectors. (#38, #41, #42)

  • html() does a better job of dealing with encodings (passing the problem on to XML::parseHTML()) instead of trying to do it itself (#25, #50).

  • html_attr() returns default value when input is NULL (#49)

  • Add missing html_node() method for session.

  • html_nodes() now returns an empty list if no elements are found (#31).

  • submit_form() converts relative paths to absolute URLs (#52). It also deals better with 0-length inputs (#29).

Reference manual

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


1.0.2 by Hadley Wickham, 7 days 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] , RStudio [cph]

Documentation:   PDF Manual  

Task views: Web Technologies and Services

MIT + file LICENSE license

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

Suggests covr, glue, knitr, readr, rmarkdown, repurrrsive, spelling, stringi, testthat, webfakes

Imported by BAwiR, BETS, BFS, BIS, BOJ, ECOTOXr, Ecfun, GetDFPData2, GetFREData, GetTDData, GoogleImage2Array, IRexamples, JumpeR, LIHKGr, MazamaCoreUtils, MediaNews, NHSDataDictionaRy, RBNZ, RGISTools, RNHANES, RNRCS, SMMT, SanFranBeachWater, SubtypeDrug, SwimmeR, aRbs, addinslist, adjustedcranlogs, aire.zmvm, algaeClassify, appler, archiveRetriever, ari, ariExtra, babsim.hospital, banxicoR, baseballDBR, cansim, choroplethr, chorrrds, clifro, cms, coalitions, colorhex, compstatr, cptec, crandep, ctrdata, do, dragon, ecochange, eph, epidata, essurvey, etl, etrader, eurlex, fastqcr, fedregs, fedstatAPIr, finreportr, fitzRoy, foghorn, foqat, frenchdata, ganalytics, gcite, genius, geniusr, gfer, googleformr, helminthR, hockeyR, hockeystick, htmldf, icpsrdata, ihpdr, kableExtra, lares, mRpostman, mediacloudr, mlbstatsR, msig, multilinguer, nettskjemar, nhanesA, nhdR, nhlscrape, noaaoceans, nse2r, nser, ojsr, osmdata, owidR, pda, pedquant, polite, postlightmercury, prisonbrief, quincunx, ralger, rawr, readOffice, readabs, readrba, regexTestR, relSim, retrosheet, rgenius, rgoogleads, ropenblas, rpubs, rstudio.prefs, rtrek, scholar, scraEP, scrappy, sejmRP, sidrar, smapr, snotelr, speaq, speech, spotifyr, squashinformr, stlcsb, textreadr, tidycensus, tidytuesdayR, tidyverse, tinyscholar, tm.plugin.factiva, traits, waccR, webchem, wikifacts, wikilake, wikipediatrend, ztype.

Depended on by BMRBr, BatchGetSymbols, FFdownload, MetaDBparse, qqr, rNOMADS.

Suggested by AMR, DOPE, ICD10gm, NBAloveR, RCzechia, RSQLite, arcpullr, autoharp, convergEU, covidregionaldata, eurostat, ezpickr, fuzzyjoin, gluedown, googleLanguageR, gt, htmltidy, ibawds, iemiscdata, mscstts, naaccr, newsanchor, nsrr, opitools, oxcgrt, preregr, rccmisc, rco, rex, rscopus, runexp, sharpshootR, soilDB, unpivotr.

See at CRAN