[問題] download 遇到網址回傳檔案大小不合

看板R_Language作者時間10年前 (2013/12/02 17:46), 編輯推噓1(103)
留言4則, 2人參與, 最新討論串1/1
[問題類型]: 程式諮詢 [軟體熟悉度]: 入門(寫過其他程式,只是對語法不熟悉) [問題敘述]: 想下載經濟部商業司的公司名冊,試著用 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

12/02 19:28, , 1F
我用你提供的程式碼,去掉網址中的斷行字元後可以下載
12/02 19:28, 1F
※ 編輯: barbq 來自: 60.251.40.253 (12/03 16:53)

12/03 16:54, , 2F
哈,沒問題了.原來是公司電腦的pdf reader中文顯示有問題,我
12/03 16:54, 2F

12/03 16:55, , 3F
一直看到亂碼誤以為下載的檔案不完整。語法裡再加mode="wb"
12/03 16:55, 3F

12/03 16:56, , 4F
、pdf reader更新一下,就沒問題了。也感謝 Wush 大大。
12/03 16:56, 4F
文章代碼(AID): #1Id5SINq (R_Language)