Skip to content

rvest

1,498 1 38 MIT
1.0.4 (20 Aug 2022) Nov 22 2014 636.2 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


156 2.2.4 (3 years ago) Dec 22 2019 compare
988 1.4.7 (1 year, 9 months ago) May 06 2012 compare
221 1.3.6 (1 year, 7 months ago) Apr 20 2015 compare
107 1.5.0 (10 months ago) Nov 09 2016 compare

Other Languages

52,519 2.32.3 (8 months ago) Feb 14 2011 compare
8,825 3.3.2 (1 year, 2 months ago) Dec 28 2012 compare
23,747 v2.1.0 (4 years ago) May 14 2018 compare
106,345 1.7.9 (a month ago) Aug 29 2014 compare
15,425 3.11.12 (12 days ago) Jul 26 2019 compare
1,101 1.4.1 (8 months ago) Feb 09 2011 compare
3,698 7.2.1 (3 months ago) Jul 03 2013 compare
4,529 10.0.0 (a month ago) Aug 28 2011 compare
13,703 0.28.1 (2 months ago) Jul 26 2019 compare
- 4.13.3 (13 days ago) Jul 26 2019 compare
2,737 5.3.0 (6 months ago) Dec 13 2022 compare
got
14,454 14.4.6 (8 days ago) Mar 27 2014 compare
7,580 v1.3.4 (5 years ago) Feb 15 2020 compare
5,577 0.14.2 (4 months ago) Jul 30 2007 compare
4,221 v0.6.1 (11 months ago) Feb 23 2022 compare
16,610 10.1.1 (3 months ago) Aug 22 2011 compare
1,637 3.3.1 (1 year, 2 months ago) Dec 11 2011 compare
28,873 1.0.0 (6 months ago) Oct 08 2011 compare
2,667 2025-02-18 (8 hours ago) Jun 06 2019 compare
1,153 1.1 (4 years ago) Jul 30 2007 compare
293 1.2.0 (2 years ago) Apr 14 2012 compare
676 2025-02-16 (a day ago) Feb 09 2017 compare
5,785 2.12.2 (2 months ago) Dec 19 2009 compare
6,173 1.18.2 (29 days ago) Jul 25 2009 compare
703 2.8.3 (8 years ago) Jul 25 2009 compare
2,048 6.0.11 (1 year, 2 months ago) Jun 15 2007 compare
1,163 1.2.3 (a month ago) Oct 31 2009 compare
5,837 0.22.0 (9 months ago) Jul 25 2009 compare
2,312 2.0.1 (5 months ago) Dec 05 2008 compare
1,094 7.45.4 (2 months ago) Feb 25 2003 compare
1,187 1.10.0 (a month ago) Jul 26 2019 compare
54,211 2.12.0 (3 months ago) Jul 26 2019 compare
4,084 1.4.1 (1 year, 3 months ago) Oct 06 2009 compare
13,780 0.10.0 (6 years ago) Feb 25 2018 compare
1,751 0.7.1 (7 months ago) Feb 23 2022 compare
23,055 7.9.1 (7 months ago) Nov 14 2011 compare
1,217 1.1.7 (4 years ago) Oct 25 2009 compare
1,186 0.3.27 (2 months ago) Mar 01 2018 compare
1,976 v7.2.3 (21 days ago) Apr 28 2019 compare
1,638 2.9.0 (10 months ago) Jun 01 2013 compare
619 1.2.1 (2 years ago) Jun 09 2011 compare
3,985 v7.2.3 (22 days ago) Sep 26 2011 compare
381 18.1.0 (6 months ago) Aug 06 2011 compare
898 1.0.2 (3 months ago) Sep 25 2013 compare
590 24.9.1 (4 months ago) Dec 28 2012 compare
717 Start (7 years ago) Feb 20 2018 compare
5,716 v0.18.0 (1 year, 10 months ago) Aug 06 2019 compare
2,039 (3 years ago) Nov 20 2016 compare
14,273 v1.10.2 (5 days ago) Aug 29 2016 compare
760 v1.3.4 (a month ago) Feb 07 2019 compare
req
4,374 v3.49.1 (a month ago) Aug 12 2023 compare
10,341 v3.0.0-alpha.6 (a month ago) May 21 2020 compare
2,191 v1.2.5 (3 years ago) Apr 29 2017 compare
699 v1.3.3 (2 months ago) Jun 08 2019 compare
505 1.1.4 (5 years ago) Apr 20 2010 compare
683 0.4.12 (1 year, 3 months ago) Jun 03 2007 compare
2,980 1.5.0 (4 months ago) Sep 04 2013 compare
202 1.3.0 (6 months ago) Jul 30 2007 compare
3,576 v2.0.15 (28 days ago) Oct 06 2013 compare
6,733 2.0.2 (7 months ago) Sep 10 2012 compare
780 0.9.2 (2 months ago) Feb 23 2022 compare
2,977 v2.2.0 (19 days ago) Jul 17 2018 compare
764 1.1 (4 years ago) Dec 28 2012 compare
1,913 1.3.0 (4 months ago) Nov 11 2011 compare
2,103 v4.4.15 (a month ago) Oct 26 2013 compare
1,741 1.0.0 (9 months ago) Apr 14 2012 compare
6,638 1.1.14 (2 years ago) Jul 26 2019 compare
247 1.34.0 (2 months ago) Feb 05 2023 compare
813 0.7.2 (15 days ago) Jul 25 2009 compare
3,218 1.6.0 (2 days ago) Sep 30 2018 compare
3,365 0.9.13 (1 year, 7 months ago) Jul 04 2017 compare
1,316 3.0.0 (2 years ago) Dec 27 2011 compare
1,754 0.8.5 (2 years ago) Oct 17 2018 compare
22 0.6.0 (1 year, 9 months ago) Jul 24 2014 compare
11,149 1.1.9 (6 years ago) Aug 24 2018 compare
1,384 v3.2.0 (10 months ago) Dec 27 2021 compare
428 0.1.3 (1 year, 6 months ago) Feb 20 2022 compare
213 1.0.0-beta8.4 (1 year, 7 months ago) Apr 18 2019 compare
3,013 0.12.1 (11 days ago) Mar 20 2015 compare
554 3.0.0 (10 months ago) May 04 2020 compare
1,335 v0.7.2 (1 year, 2 months ago) Mar 16 2013 compare
356 v3.2.3 (21 days ago) Apr 18 2022 compare
Was this page helpful?