在R中导出CSV而不使用csv链接

时间:2015-11-18 10:12:54

标签: r csv

除了"下载所有记录"我想在小excel图标下面获取csv文件记录。来自R的this url

文件没有附加csv / excel链接,因此我没有使用read.csv选项的网址。

我的尝试:

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

但是一切都是徒劳的,请帮忙。

1 个答案:

答案 0 :(得分:0)

你走了:

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)

运行

head(data)

给出

  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