R: Download image using rvest

前端 未结 2 1546
南旧
南旧 2020-12-15 00:12

I\'m attempting to download a png image from a secure site through R.

To access the secure site I used Rvest which worked well.

So

2条回答
  •  别那么骄傲
    2020-12-15 00:28

    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))
    

提交回复
热议问题