Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download png/jpg with R

Tags:

r

i would like to download all of images from this site but after downloading photos all are corrupted. What i should do to download them successfully?

My code:

library(XML)
dir.create('c:/photos')
urls<-paste("http://thedevilsguard.tumblr.com/page/",1:1870,sep="")
doc<-htmlParse(urls[1])
links<-unique(unlist(xpathApply(doc,'//div[@class="timestamp"]/a',xmlGetAttr,'href')))
for (i in 1:length(links)){
  doc2<-htmlParse(links[i])
  link<-xpathApply(doc2,'//div[@class="centre photopage"]//p//img',xmlGetAttr,'src')[[1]][1]
  download.file(link,paste("C:/photos/",basename(link),""))
}
like image 771
Maciej Avatar asked Mar 11 '12 13:03

Maciej


2 Answers

First, try and download one. Do this:

link = "http://29.media.tumblr.com/tumblr_m0q2g8mhGK1qk6uvyo1_500.png"
download.file(link,basename(link))

Does that work?

I notice its a PNG and NOT a JPEG, so maybe you are trying to read it in as a JPEG.

like image 189
Spacedman Avatar answered Oct 21 '22 10:10

Spacedman


So it looks you are under Windows. When you download binary files, you have to specify the mode to be binary, e.g.

download.file(link, ..., mode = 'wb')

see ?download.file for details.

like image 29
Yihui Xie Avatar answered Oct 21 '22 10:10

Yihui Xie