How to download a file from the Internet via R

I have a url and I want to upload the file through R, I notice that it download.filewill be useful, but my problem seems different:

url <- "http://journal.gucas.ac.cn/CN/article/downloadArticleFile.do?attachType=PDF&id=11771"
destfile <- "myfile.pdf"
download.file(url, destfile)

This does not work! I notice that if mine urlhas a form xxx.pdf, then the code above is not a problem, otherwise the uploaded file is damaged.

Does anyone know how to solve this problem?

+2
source share
1 answer

Mode setting may be required to process the file as binary data when it is saved. If I leave this argument, I get an empty file, but this method works for me:

url <- "http://journal.gucas.ac.cn/CN/article/downloadArticleFile.do?
attachType=PDF&id=11771"
destfile <- "myfile.pdf"
download.file(url, destfile, mode="wb")
+1
source

Source: https://habr.com/ru/post/1528187/


All Articles