1

I want to get the csv file records beneath the small excel icon besides "Download All Records" from this url in R.

There is no csv/excel link attached to the file, hence I don't have a url to use the read.csv option.

My try:

tmpFile <- tempfile()
download.file(url, destfile = tmpFile, method="auto")
url.data <- read.csv(tmpFile)
url.data

But all in vain, any help please.

Vasim
  • 3,052
  • 3
  • 35
  • 56

1 Answers1

0

Here you go:

library(RCurl)

url <- "http://www.bseindia.com/markets/equity/EQReports/HighLow.aspx"
curl = getCurlHandle()
curlSetOpt(cookiejar = 'cookies.txt', followlocation = TRUE, autoreferer = TRUE, curl = curl)
html <- getURL(url, curl = curl)

viewstate <- as.character(sub('.*id="__VIEWSTATE" value="([0-9a-zA-Z+/=]*).*', '\\1', html))
viewstate.generator <- as.character(sub('.*id="__VIEWSTATEGENERATOR" value="([0-9a-zA-Z+/=]*).*', '\\1', html))
event.validation <- as.character(sub('.*id="__EVENTVALIDATION" value="([0-9a-zA-Z+/=]*).*', '\\1', html))

params <- list(
  '__EVENTTARGET'        = '',
  '__EVENTARGUMENT'      = '',
  '__VIEWSTATE'          = viewstate,
  '__VIEWSTATEGENERATOR' = viewstate.generator,
  '__EVENTVALIDATION'    = event.validation,
  'myDestination'        = '#',
  'WINDOW_NAMER'        = '1',
  'ctl00$ContentPlaceHolder1$hdnCode'       = '',
  'ctl00$ContentPlaceHolder1$hdnScrip'      = '',
  'ctl00$ContentPlaceHolder1$btnDownload.x' = '6',
  'ctl00$ContentPlaceHolder1$btnDownload.y' = '7',
  'ctl00$ContentPlaceHolder1$chk'           = 'rdnScrip',
  'ctl00$ContentPlaceHolder1$Hidden1'       = '',
  'ctl00$ContentPlaceHolder1$GetQuote1_smartSearch' = 'Enter Security Name / Code / ID',
  'ctl00$ContentPlaceHolder1$ddlType'               = 'AllMkt',
  'ctl00$ContentPlaceHolder1$ddlGrp'                = 'A',
  'ctl00$ContentPlaceHolder1$ddlIndx'               = 'S&P BSE SENSEX'
)

html = postForm(url, .params = params, curl = curl)
dummy.csv <- rawToChar(html)
data <- read.csv(text = dummy.csv)

Running

head(data)

gives

  Security.Code Security.Name   LTP X52.Weeks.High Previous.52.Weeks.High
1        532875          ADSL 42.95          45.10                  44.70
2        526628       AJWAFUN 14.28          14.28                  13.60
3        532878          ALPA 46.45          46.45                  44.25
4        506248       AMNPLST 22.65          22.65                  21.75
5        531127     ANARINDUS 26.90          26.90                  26.30
6        530721        ANGIND 49.85          52.60                  51.70
  Previous.52.Weeks.High.Date All.Time.High.Price All.Time.High.Date Group
1                 17 Nov 2015              1129.0        08 Jan 2008     B
2                 17 Nov 2015                90.0        13 Feb 1995     P
3                 17 Nov 2015                65.0        06 Aug 2007     T
4                 17 Nov 2015               208.9        19 Feb 2015    XT
5                 17 Nov 2015                41.0        10 Mar 2011    XD
6                 20 Jan 2015               414.0        15 May 2006     B
rbm
  • 3,243
  • 2
  • 17
  • 28
  • I should point out that the credit should be given to http://stackoverflow.com/questions/15853204/how-to-login-and-then-download-a-file-from-aspx-web-pages-with-r - i merely reverse-engineered the POST request via Chrome. – rbm Nov 18 '15 at 13:37