Reputation: 149
I am having problem downloading a csv file from internet.
I tried the following code, but I wasn't able to make it work.
myurl <- "http://unstats.un.org/unsd/mdg/Handlers/ExportHandler.ashx?Type=Csv&Series=761"
download.file(myurl, destfile="./test.csv",method="curl")
Instead I received the following output.
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0100 16313 100 16313 0 0 11961 0 0:00:01 0:00:01 --:--:-- 11959100 16313 100 16313 0 0 11960 0 0:00:01 0:00:01 --:--:-- 11959
When I manually download, a file like this is generated "MDG_Export_20150821_224828123.csv". The server generates a new file name (MDG_Export + date + time).
Is there anyway I can download this from my R program because the manual download is not an option in my project?
Thank you for your help.
Upvotes: 0
Views: 1770
Reputation: 263301
Try this:
myurl <- "http://unstats.un.org/unsd/mdg/Handlers/ExportHandler.ashx?Type=Csv&Series=761&filename=MDG_Export_20150821_224828123.zip"
myzip <- unz(myurl, filename='MDG_Export_20150821_224828123.zip')
download.file(myurl, destfile="./test.zip",method="curl")
You will then need to expand the file with unzip
Upvotes: 2