Skip to content

rvest

1,428 1 24 MIT
1.0.3 (17 Oct 2021) Nov 22 2014 488.3 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


150 2.2.4 (2 years ago) Dec 22 2019 compare
975 1.4.7 (6 months ago) May 06 2012 compare
213 1.3.5 (7 months ago) Apr 20 2015 compare
101 1.4.0 (1 year, 2 months ago) Nov 09 2016 compare

Other Languages

50,569 2.31.0 (6 months ago) Feb 14 2011 compare
21,215 v2.1.0 (3 years ago) May 14 2018 compare
102,345 1.6.0 (a month ago) Aug 29 2014 compare
8,535 3.3.2 (3 months ago) Dec 28 2012 compare
3,463 7.1.2 (4 months ago) Jul 03 2013 compare
1,058 1.3.0 (2 months ago) Feb 09 2011 compare
4,034 9.0.0 (6 months ago) Aug 28 2011 compare
14,116 3.9.0 (11 days ago) Jul 26 2019 compare
2,484 4.9.3 (4 months ago) Dec 13 2022 compare
got
13,576 13.0.0 (6 months ago) Mar 27 2014 compare
7,476 v1.3.4 (3 years ago) Feb 15 2020 compare
- 4.12.2 (7 months ago) Jul 26 2019 compare
16,449 8.1.2 (3 months ago) Aug 22 2011 compare
1,599 3.2.0 (1 year, 12 days ago) Dec 11 2011 compare
5,269 0.13.0 (1 year, 6 months ago) Jul 30 2007 compare
27,140 1.0.0-rc.12 (22 days ago) Oct 08 2011 compare
11,446 0.25.1 (26 days ago) Jul 26 2019 compare
2,296 2023-11-24 (4 days ago) Jun 06 2019 compare
1,072 1.1 (3 years ago) Jul 30 2007 compare
622 2023-11-24 (4 days ago) Feb 09 2017 compare
275 1.2.0 (1 year, 1 month ago) Apr 14 2012 compare
5,578 2.7.11 (2 months ago) Dec 19 2009 compare
6,077 1.15.5 (11 days ago) Jul 25 2009 compare
695 2.8.3 (6 years ago) Jul 25 2009 compare
1,147 0.104.0 (a month ago) Oct 31 2009 compare
1,734 6.0.10 (1 year, 6 months ago) Jun 15 2007 compare
2,243 2.0.0 (11 months ago) Dec 05 2008 compare
5,721 0.21.0 (10 months ago) Jul 25 2009 compare
49,364 2.11.0 (2 months ago) Jul 26 2019 compare
1,020 1.8.1 (7 months ago) Jul 26 2019 compare
1,019 7.45.2 (11 months ago) Feb 25 2003 compare
13,416 0.10.0 (4 years ago) Feb 25 2018 compare
4,045 1.4.1 (12 days ago) Oct 06 2009 compare
22,789 7.8.0 (3 months ago) Nov 14 2011 compare
1,217 1.1.7 (3 years ago) Oct 25 2009 compare
382 18.1.0 (20 days ago) Aug 06 2011 compare
1,819 v7.0.0-RC1 (22 days ago) Apr 28 2019 compare
2,734 1.4.3 (2 months ago) Sep 04 2013 compare
877 0.11.0 (2 years ago) Sep 25 2013 compare
655 0.4.12 (10 days ago) Jun 03 2007 compare
3,867 v7.0.0-RC1 (14 days ago) Sep 26 2011 compare
1,381 2.8.1 (6 months ago) Jun 01 2013 compare
605 1.2.1 (1 year, 4 months ago) Jun 09 2011 compare
565 23.11.0 (25 days ago) Dec 28 2012 compare
6,541 1.4.0 (10 months ago) Sep 10 2012 compare
13,155 v1.8.1 (9 months ago) Aug 29 2016 compare
645 Start (5 years ago) Feb 20 2018 compare
609 v1.2.5 (a month ago) Jun 08 2019 compare
req
3,766 v3.42.2 (9 days ago) Apr 29 2023 compare
8,664 v2.10.0 (a month ago) Aug 05 2018 compare
2,080 v1.2.5 (1 year, 10 months ago) Apr 29 2017 compare
649 v1.3.0 (10 months ago) Feb 07 2019 compare
5,513 v0.18.0 (8 months ago) Aug 06 2019 compare
2,021 (2 years ago) Nov 20 2016 compare
895 0.3.17 (a month ago) Mar 01 2018 compare
506 1.1.4 (3 years ago) Apr 20 2010 compare
3,536 v2.0.9 (20 days ago) Oct 06 2013 compare
159 1.2.2 (5 months ago) Jul 30 2007 compare
716 1.1 (3 years ago) Dec 28 2012 compare
1,916 1.2.1 (1 year, 8 months ago) Nov 11 2011 compare
2,829 v2.1.0 (5 months ago) Jul 17 2018 compare
1,737 0.3.2 (3 years ago) Apr 14 2012 compare
2,041 v4.4.8 (6 months ago) Oct 26 2013 compare
5,654 1.1.14 (1 year, 4 months ago) Jul 26 2019 compare
1,298 3.0.0 (1 year, 3 months ago) Dec 27 2011 compare
769 0.7.0 (10 months ago) Jul 25 2009 compare
1,701 0.8.5 (1 year, 2 months ago) Oct 17 2018 compare
2,864 1.4.1 (2 months ago) Sep 30 2018 compare
22 0.6.0 (7 months ago) Jul 24 2014 compare
3,118 0.9.13 (4 months ago) Jul 04 2017 compare
2,968 0.12.0 (6 years ago) Mar 20 2015 compare
10,120 1.1.9 (5 years ago) Aug 24 2018 compare
1,271 2.0.1 (9 months ago) Dec 27 2021 compare
224 1.27.1 (3 months ago) Feb 05 2023 compare
443 2.0.0 (5 months ago) May 04 2020 compare
1,312 v0.7.1 (3 months ago) Mar 16 2013 compare
186 1.0.0-beta8.4 (5 months ago) Apr 18 2019 compare
386 0.1.3 (3 months ago) Feb 20 2022 compare
277 v1.3.0 (a month ago) Apr 18 2022 compare