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.
library(rvest) lego_movie <- read_html("http://www.imdb.com/title/tt1490017/") rating <- lego_movie %>% html_nodes("strong span") %>% html_text() %>% as.numeric() rating #>  7.7 cast <- lego_movie %>% html_nodes("#titleCast .primary_photo img") %>% html_attr("alt") cast #>  "Will Arnett" "Elizabeth Banks" "Craig Berry" "Alison Brie" #>  "David Burrows" "Anthony Daniels" "Charlie Day" "Amanda Farinos" #>  "Keith Ferguson" "Will Ferrell" "Will Forte" "Dave Franco" #>  "Morgan Freeman" "Todd Hansen" "Jonah Hill" poster <- lego_movie %>% html_nodes(".poster img") %>% html_attr("src") poster #>  "https://m.media-amazon.com/images/M/MV5BMTg4MDk1ODExN15BMl5BanBnXkFtZTgwNzIyNjg3MDE@._V1_UX182_CR0,0,182,268_AL_.jpg"
Install the release version from CRAN:
Or the development version from GitHub
# install.packages("devtools") devtools::install_github("tidyverse/rvest")
Once you have read a HTML document with
read_html(), you can:
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
(You can also use rvest with XML files: parse with
xml(), then extract components using
Parse tables into data frames with
Extract, modify and submit forms with
Detect and repair encoding problems with
Navigate around a website as if you’re in a browser with
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.
- Python: RoboBrowser, Beautiful Soup.
Code of Conduct
Please note that the rvest project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.