kay dee kay dee - 1 year ago 112
R Question

Error downloading a csv in zip from website with get in R

I would like to read data from nse-india.com to R using download.file() as shown below.

url = 'http://www.nseindia.com/content/historical/EQUITIES/2014/SEP/cm24SEP2014bhav.csv.zip'
temp = tempfile()
download.file(url, destfile = temp,method = 'wget')

It throws up following error:

SYSTEM_WGETRC = c:/progra~1/wget/etc/wgetrc
syswgetrc = C:\PROGRA~2\GnuWin32/etc/wgetrc
--2014-09-28 21:19:26-- http://www.nseindia.com/content/historical/EQUITIES/2014/SEP/cm24SEP2014bhav.csv.zip
Resolving www.nseindia.com...,
Connecting to www.nseindia.com||:80... connected.
HTTP request sent, awaiting response... 403 Forbidden
2014-09-28 21:19:26 ERROR 403: Forbidden.

Warning messages:
1: running command 'wget "http://www.nseindia.com/content/historical/EQUITIES/2014/SEP/cm24SEP2014bhav.csv.zip" -O "C:\Users\ITITHI~1\AppData\Local\Temp\Rtmp2fjADx\file1fb02375882"' had status 1
2: In download.file(url, destfile = temp, method = "wget") :
download had nonzero exit status

Please let me know anyway to fix this.

EDIT: Or any other method to download the file from within R would also be great.

Answer Source

You need to set a browser-like user agent string so the site thinks you're a browser vs an automated scraper/downloader robot:

library(httr) # >=v0.5

    user_agent("Mozilla/5.0"), write_disk("cm24SEP2014bhav.csv.zip"))

## Response [http://www.nseindia.com/content/historical/EQUITIES/2014/SEP/cm24SEP2014bhav.csv.zip]
##   Date: 2014-09-28 23:53
##   Status: 200
##   Content-type: application/zip
##   Size: 58.2 kB
## <ON DISK>  cm24SEP2014bhav.csv.zip
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download