r - 用 R 下载 png/jpg

标签 r

我想从 this site 下载所有图片但下载照片后,所有的都已损坏。我应该怎么做才能成功下载它们?

我的代码:

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),""))
}

最佳答案

所以看起来你在Windows下。下载二进制文件时,必须指定模式为二进制,例如

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

?download.file详情。

关于r - 用 R 下载 png/jpg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9655361/

相关文章:

r - 将每日级别的数据聚合到 R 中的每周级别

r - Shiny 的应用程序错误源调试

r - 如何阻止pdf中的bookdown表格 float 到页面底部?

r - 使用存储在变量中的列名连接数据表

r从数据框中提取列表元素

r - 为时间序列创建 future 值矩阵

r - 负二项式 glmer 的模型收敛警告

r - 将 "set in a string list"优化为 "set as a matrix"操作

r - 将用户在 rcpp 中创建的 C++ 函数作为参数传递

r - FUN(X[[i]], ...) 错误 : object not found when adding geom_text with facet