[问题类型]:
程式谘询
[软体熟悉度]:
入门(写过其他程式,只是对语法不熟悉)
[问题叙述]:
想下载经济部商业司的公司名册,试着用 download.file 的指令却会收到以下错误
Warning message:
In download.file(URL, destfile) :
downloaded length 78854 != reported length 200
[执行环境]:
win7 + Rstudio 0.97.551
完整的商业司公司名册列表:
http://gcis.nat.gov.tw/pub/cmpy/reportCity.jsp
[程式范例]:
> URL <-
"
http://gcis.nat.gov.tw/pub/cmpy/reportAction.do?method=report&reportClass=cmpyCity&subPath=10210&fileName=376570000Asetup10210.pdf"
> destfile <- "test.pdf"
> download.file(URL, destfile)
trying URL '略'
Content type 'application/pdf;charset=Big5' length 200 bytes
opened URL
downloaded 77 Kb
Warning message:
In download.file(URL, destfile) :
downloaded length 78854 != reported length 200
[测试过程]
1. 原以为是网址带参数的问题,但测试档案管理局的网址可正常下载
> URL =
"
http://www.archives.gov.tw/chinese_archival/Download_File.ashx?id=9103"
> destfile <- "test.pdf"
> download.file(URL, destfile)
trying URL '略'
Content type 'application/octet-stream' length 31627122 bytes (30.2 Mb)
opened URL
2. 怀疑网址被 redirect 造成,但这个 sourceforge 的网址也可下载
> URL =
"
http://sourceforge.net/projects/portableapps/files/PeaZip%20Portable/Additional%20Versions/PeaZipPortable_4.1.paf.exe/download?use_mirror=nchc"
> destfile <- "test.exe"
> download.file(URL, destfile)
trying URL '略'
Content type 'application/octet-stream' length 5943912 bytes (5.7 Mb)
opened URL
3. 根据 google 建议加过 mode = "wb" 或改用 install.packages("downloader")
也都是一样的错误
install.packages("downloader")
library(downloader)
download("略",
"downloader.pdf", mode = "wb")
4. 再看一次 error,应该是 header 的问题
Content type 'application/pdf;charset=Big5' length 200 bytes
header 回传的档案大小跟实际档案大小不合
翻了 help 没找到可 skip header 的方式
在此请教各位高手
[关键字]:
R, download, 下载, 语法, syntax
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 118.163.170.73
1F:推 Wush978:我用你提供的程式码,去掉网址中的断行字元後可以下载 12/02 19:28
※ 编辑: barbq 来自: 60.251.40.253 (12/03 16:53)
2F:→ barbq:哈,没问题了.原来是公司电脑的pdf reader中文显示有问题,我 12/03 16:54
3F:→ barbq:一直看到乱码误以为下载的档案不完整。语法里再加mode="wb" 12/03 16:55
4F:→ barbq:、pdf reader更新一下,就没问题了。也感谢 Wush 大大。 12/03 16:56