Skip to content

rvest

1,387 1 17 MIT
1.0.3 (17 Oct 2021) Nov 22 2014 510.4 thousand (month)

rvest is a popular R library for web scraping and parsing HTML and XML documents. It is built on top of the xml2 and httr libraries and provides a simple and consistent API for interacting with web pages.

One of the main advantages of using rvest is its simplicity and ease of use. It provides a number of functions that make it easy to extract information from web pages, even for those who are not familiar with web scraping. The html_nodes and html_node functions allow you to select elements from an HTML document using CSS selectors, similar to how you would select elements in JavaScript.

rvest also provides functions for interacting with forms, including html_form, set_values, and submit_form functions. These functions make it easy to navigate through forms and submit data to the server, which can be useful when scraping sites that require authentication or when interacting with dynamic web pages.

rvest also provides functions for parsing XML documents. It includes xml_nodes and xml_node functions, which also use CSS selectors to select elements from an XML document, as well as xml_attrs and xml_attr functions to extract attributes from elements.

Another advantage of rvest is that it provides a way to handle cookies, so you can keep the session alive while scraping a website, and also you can handle redirections with handle_redirects

Example Use


library("rvest")

# Rvest can use basic HTTP client to download remote HTML:
tree <- read_html("http://webscraping.fyi/lib/r/rvest")
# or read from string:
tree <- read_html('
<div class="products">
  <a href="/product/1">Cat Food</a>
  <a href="/product/2">Dog Food</a>
</div>
')

# to parse HTML trees with rvest we use r pipes (the %>% symbol) and html_element function:
# we can use css selectors:
print(tree %>% html_element(".products>a") %>% html_text())
# "[1] "\nCat Food\nDog Food\n""

# or XPath:
print(tree %>% html_element(xpath="//div[@class='products']/a") %>% html_text())
# "[1] "\nCat Food\nDog Food\n""

# Additionally rvest offers many quality of life functions:
# html_text2 - removes trailing and leading spaces and joins values
print(tree %>% html_element("div") %>% html_text2())
# "[1] "Cat Food Dog Food""

# html_attr - selects element's attribute:
print(tree %>% html_element("div") %>% html_attr('class'))
# "products"

Alternatives / Similar


145 2.2.4 (2 years ago) Dec 22 2019 compare
966 1.4.4 (9 months ago) May 06 2012 compare
205 1.3.3 (2 years ago) Apr 20 2015 compare
94 1.3 (1 year, 3 months ago) Nov 09 2016 compare

Other Languages

49,097 2.28.2 (a month ago) Feb 14 2011 compare
18,958 v2.1.0 (2 years ago) May 14 2018 compare
8,082 3.3.0 (a month ago) Dec 28 2012 compare
98,234 1.2.2 (a month ago) Aug 29 2014 compare
3,229 7.1.2 (3 months ago) Jul 03 2013 compare
1,022 1.2.4 (3 months ago) Feb 09 2011 compare
3,727 8.0.1 (2 months ago) Aug 28 2011 compare
7,284 v1.3.4 (3 years ago) Feb 15 2020 compare
got
12,497 12.5.3 (2 months ago) Mar 27 2014 compare
13,292 3.8.4 (11 days ago) Jul 26 2019 compare
2,289 4.9.2 (2 months ago) Dec 13 compare
- 4.11.2 (23 days ago) Jul 26 2019 compare
16,224 8.0.6 (2 months ago) Aug 22 2011 compare
25,854 1.0.0-rc.12 (3 months ago) Oct 08 2011 compare
5,059 0.13.0 (9 months ago) Jul 30 2007 compare
1,567 3.2.0 (3 months ago) Dec 11 2011 compare
1,857 2023-02-23 (22 hours ago) Jun 06 2019 compare
1,010 1.1 (2 years ago) Jul 30 2007 compare
10,106 0.23.3 (a month ago) Jul 26 2019 compare
571 2023-02-22 (2 days ago) Feb 09 2017 compare
268 1.2.0 (3 months ago) Apr 14 2012 compare
5,980 1.14.2 (10 days ago) Jul 25 2009 compare
5,444 2.7.4 (a month ago) Dec 19 2009 compare
697 2.8.3 (6 years ago) Jul 25 2009 compare
1,550 6.0.10 (9 months ago) Jun 15 2007 compare
1,122 0.99.0 (20 days ago) Oct 31 2009 compare
5,644 0.21.0 (a month ago) Jul 25 2009 compare
2,183 2.0.0 (a month ago) Dec 05 2008 compare
949 7.45.2 (2 months ago) Feb 25 2003 compare
884 1.7.0 (3 months ago) Jul 26 2019 compare
46,263 2.8.0 (22 days ago) Jul 26 2019 compare
3,989 1.4.0 (2 years ago) Oct 06 2009 compare
12,968 0.10.0 (4 years ago) Feb 25 2018 compare
22,372 7.5.0 (5 months ago) Nov 14 2011 compare
383 18.1.0 (6 months ago) Aug 06 2011 compare
1,210 1.1.7 (2 years ago) Oct 25 2009 compare
579 1.2.1 (7 months ago) Jun 09 2011 compare
872 0.11.0 (2 years ago) Sep 25 2013 compare
1,687 v6.2.6 (24 days ago) Apr 28 2019 compare
3,742 v6.2.5 (a month ago) Sep 26 2011 compare
552 22.2.0 (1 year, 15 days ago) Dec 28 2012 compare
1,207 2.7.6 (6 months ago) Jun 01 2013 compare
5,227 v0.17.0 (16 days ago) Jul 27 2019 compare
1,937 v1.2.5 (1 year, 1 month ago) Apr 29 2017 compare
12,294 v1.8.1 (5 days ago) Aug 29 2016 compare
592 Start (5 years ago) Feb 20 2018 compare
req
3,073 v3.32.1 (2 days ago) Aug 21 2022 compare
2,008 (1 year, 9 months ago) Nov 20 2016 compare
7,360 v2.7.0 (1 year, 3 months ago) Jan 25 2018 compare
552 v1.2.4 (8 days ago) Jun 08 2019 compare
570 v1.3.0 (28 days ago) Feb 07 2019 compare
6,334 1.4.0 (a month ago) Sep 10 2012 compare
642 0.4.10 (1 year, 5 months ago) Jun 03 2007 compare
505 1.1.4 (3 years ago) Apr 20 2010 compare
750 0.3.12 (a month ago) Mar 01 2018 compare
698 1.1 (2 years ago) Dec 28 2012 compare
2,598 1.4.1 (14 days ago) Sep 04 2013 compare
3,501 v2.0.5 (4 months ago) Oct 06 2013 compare
1,897 1.2.1 (10 months ago) Nov 11 2011 compare
2,691 v2.0.1 (1 year, 2 months ago) Jul 17 2018 compare
1,733 0.3.2 (3 years ago) Apr 14 2012 compare
1,956 v4.4.7 (2 months ago) Oct 26 2013 compare
4,911 1.1.14 (7 months ago) Jul 26 2019 compare
156 1.11.1 (9 hours ago) Feb 05 compare
2,671 1.4.0 (3 years ago) Sep 30 2018 compare
1,281 3.0.0 (6 months ago) Dec 27 2011 compare
744 0.7.0 (a month ago) Jul 25 2009 compare
1,642 0.8.5 (5 months ago) Oct 17 2018 compare
9,417 1.1.9 (4 years ago) Aug 24 2018 compare
2,914 0.9.12 (a month ago) Jul 04 2017 compare
2,907 0.12.0 (5 years ago) Mar 20 2015 compare
160 1.0.0-beta8.3 (4 months ago) Apr 18 2019 compare
1,121 2.0.1 (6 days ago) Dec 27 2021 compare
355 1.0.2 (2 months ago) May 04 2020 compare
1,286 v0.6.3 (10 months ago) Mar 16 2013 compare
22 0.5.0 (7 months ago) Jul 24 2014 compare
341 0.1.2 (2 years ago) Feb 20 2022 compare
128 v1.0.0 (15 days ago) Apr 18 2022 compare