Skip to content

rvest

1,485 1 23 MIT
1.0.4 (20 Aug 2022) Nov 22 2014 483.1 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


153 2.2.4 (3 years ago) Dec 22 2019 compare
982 1.4.7 (1 year, 2 months ago) May 06 2012 compare
219 1.3.6 (1 year, 10 days ago) Apr 20 2015 compare
102 1.4.2 (1 year, 1 month ago) Nov 09 2016 compare

Other Languages

51,729 2.32.3 (a month ago) Feb 14 2011 compare
8,709 3.3.2 (7 months ago) Dec 28 2012 compare
22,459 v2.1.0 (4 years ago) May 14 2018 compare
104,592 1.7.2 (a month ago) Aug 29 2014 compare
3,600 7.1.2 (1 year, 6 days ago) Jul 03 2013 compare
1,082 1.4.1 (a month ago) Feb 09 2011 compare
4,345 9.1.0 (6 months ago) Aug 28 2011 compare
14,776 3.9.5 (2 months ago) Jul 26 2019 compare
- 4.12.3 (5 months ago) Jul 26 2019 compare
got
14,053 14.4.1 (a month ago) Mar 27 2014 compare
2,614 5.2.2 (2 months ago) Dec 13 2022 compare
7,554 v1.3.4 (4 years ago) Feb 15 2020 compare
12,702 0.27.0 (4 months ago) Jul 26 2019 compare
5,416 0.13.0 (2 years ago) Jul 30 2007 compare
16,564 9.0.2 (2 months ago) Aug 22 2011 compare
3,467 v0.6.1 (4 months ago) Feb 23 2022 compare
28,045 1.0.0-rc.12 (8 months ago) Oct 08 2011 compare
1,619 3.3.1 (7 months ago) Dec 11 2011 compare
2,488 2024-05-09 (2 months ago) Jun 06 2019 compare
1,105 1.1 (4 years ago) Jul 30 2007 compare
651 2024-06-25 (20 days ago) Feb 09 2017 compare
287 1.2.0 (1 year, 8 months ago) Apr 14 2012 compare
5,702 2.9.2 (27 days ago) Dec 19 2009 compare
6,118 1.16.6 (a month ago) Jul 25 2009 compare
699 2.8.3 (7 years ago) Jul 25 2009 compare
1,867 6.0.11 (7 months ago) Jun 15 2007 compare
1,154 0.110.0 (4 months ago) Oct 31 2009 compare
2,279 2.0.0 (1 year, 6 months ago) Dec 05 2008 compare
5,776 0.22.0 (2 months ago) Jul 25 2009 compare
1,101 1.9.1 (3 months ago) Jul 26 2019 compare
51,636 2.11.2 (2 months ago) Jul 26 2019 compare
1,058 7.45.3 (4 months ago) Feb 25 2003 compare
13,615 0.10.0 (5 years ago) Feb 25 2018 compare
4,073 1.4.1 (7 months ago) Oct 06 2009 compare
1,052 0.3.21 (4 months ago) Mar 01 2018 compare
22,795 7.8.0 (10 months ago) Nov 14 2011 compare
1,633 0.7.0 (12 days ago) Feb 23 2022 compare
1,220 1.1.7 (3 years ago) Oct 25 2009 compare
383 18.1.0 (6 months ago) Aug 06 2011 compare
1,898 v7.1.2 (17 days ago) Apr 28 2019 compare
1,516 2.9.0 (3 months ago) Jun 01 2013 compare
3,920 v7.1.1 (a month ago) Sep 26 2011 compare
609 1.2.1 (2 years ago) Jun 09 2011 compare
887 1.0.0 (17 days ago) Sep 25 2013 compare
585 23.11.0 (8 months ago) Dec 28 2012 compare
684 Start (6 years ago) Feb 20 2018 compare
5,663 v0.18.0 (1 year, 3 months ago) Aug 06 2019 compare
2,029 (3 years ago) Nov 20 2016 compare
13,755 v1.9.2 (2 months ago) Aug 29 2016 compare
710 v1.3.2 (21 days ago) Feb 07 2019 compare
req
4,126 v3.43.7 (a month ago) Jun 28 2023 compare
9,632 v2.13.1 (2 months ago) Oct 28 2018 compare
2,146 v1.2.5 (2 years ago) Apr 29 2017 compare
659 (3 months ago) Jun 08 2019 compare
673 0.4.12 (7 months ago) Jun 03 2007 compare
507 1.1.4 (4 years ago) Apr 20 2010 compare
6,650 2.0.1 (20 days ago) Sep 10 2012 compare
2,884 1.4.3 (9 months ago) Sep 04 2013 compare
181 1.2.3 (5 months ago) Jul 30 2007 compare
3,564 v2.0.11 (3 months ago) Oct 06 2013 compare
2,907 v2.1.1 (7 months ago) Jul 17 2018 compare
738 1.1 (3 years ago) Dec 28 2012 compare
1,919 1.2.1 (2 years ago) Nov 11 2011 compare
1,738 1.0.0 (2 months ago) Apr 14 2012 compare
2,072 v4.4.11 (a month ago) Oct 26 2013 compare
541 0.8.2 (3 months ago) Feb 23 2022 compare
6,060 1.1.14 (1 year, 11 months ago) Jul 26 2019 compare
795 0.7.1 (5 months ago) Jul 25 2009 compare
1,308 3.0.0 (1 year, 10 months ago) Dec 27 2011 compare
3,048 1.5.0 (5 months ago) Sep 30 2018 compare
22 0.6.0 (1 year, 2 months ago) Jul 24 2014 compare
240 1.33.0 (4 months ago) Feb 05 2023 compare
3,257 0.9.13 (11 months ago) Jul 04 2017 compare
1,737 0.8.5 (1 year, 10 months ago) Oct 17 2018 compare
2,994 0.12.0 (6 years ago) Mar 20 2015 compare
1,337 v3.2.0 (3 months ago) Dec 27 2021 compare
10,672 1.1.9 (5 years ago) Aug 24 2018 compare
506 3.0.0 (3 months ago) May 04 2020 compare
1,328 v0.7.2 (7 months ago) Mar 16 2013 compare
200 1.0.0-beta8.4 (1 year, 17 days ago) Apr 18 2019 compare
413 0.1.3 (11 months ago) Feb 20 2022 compare
311 v1.9.3 (10 days ago) Apr 18 2022 compare
Was this page helpful?