user2128057
user2128057

Reputation: 13

How to download a redirected image link in R?

Example link: http://www.espncricinfo.com/inline/content/image/252437.html?alt=1

I tried download.file() and getURL() methods, but it didn't work.

Upvotes: 1

Views: 74

Answers (1)

r2evans
r2evans

Reputation: 160437

One option:

library(httr)
img <- httr::GET("http://www.espncricinfo.com/inline/content/image/252437.html?alt=1")\
writeBin(img$content, "localfile.jpg")

You can extract the redirected filename with img$url. In this case, you can also find mirrors for it under img$all_headers, though that isn't necessarily always the case. Try str(img) to see more of the response type that GET returns.

Upvotes: 1

Related Questions