简体   繁体   中英

Download png/jpg with 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),""))
}

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

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

see ?download.file for details.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM