R: Download image using rvest
Here's one example to download the R logo into the current directory.
library(rvest)
url <- "https://www.r-project.org"
imgsrc <- read_html(url) %>%
html_node(xpath = '//*/img') %>%
html_attr('src')
imgsrc
# [1] "/Rlogo.png"
# side-effect!
download.file(paste0(url, imgsrc), destfile = basename(imgsrc))
EDIT
Since authentication is involved, Austin's suggestion of using a session is certainly required. Try this:
library(rvest)
library(httr)
sess <- html_session(url)
imgsrc <- sess %>%
read_html() %>%
html_node(xpath = '//*/img') %>%
html_attr('src')
img <- jump_to(sess, paste0(url, imgsrc))
# side-effect!
writeBin(img$response$content, basename(imgsrc))
Try this example below:
library(rvest); library(dplyr)
url <- "http://www.calacademy.org/explore-science/new-discoveries-an-alaskan-butterfly-a-spider-physicist-and-more"
webpage <- html_session(url)
link.titles <- webpage %>% html_nodes("img")
img.url <- link.titles[13] %>% html_attr("src")
download.file(img.url, "test.jpg", mode = "wb")
You now have "test.jpg" which is the picture: