2015-05-12 67 views
1

提交按鈕,我想自動在R中的以下任務:下載CSV文件中的R

轉到與歷史數據如下頁面: http://www.ariva.de/XXX/historische_kurse, 其中XXX代表的一些股票,例如像DBX0BT

因此,在這種情況下,網址是: http://www.ariva.de/DBX0BT/historische_kurse

在右側底部有一個按鈕Download

我想創建一個函數,它接受作爲輸入:(馮)

  • 到日期(之二)
  • 路徑

    • 股票
    • 從日期

    ,然後通過按「Download」按鈕將結果csv文件下載到路徑給出的位置路徑。

    我的問題
    我該如何在R中做到這一點?

  • 回答

    3

    只要您有一個WKNISIN的列表,您可以運行它並讀取/下載文件。你也可以刮掉WKN/ISINs,但是從這個網頁會更加複雜,我假設你可以訪問這些信息。

    library(XML) 
    
    wkn<-c("DBX0BT","865985") #some stock IDs (WKN) for your targets 
    date.F<-"12.5.2014" #from when for historical data 
    date.T<-"12.5.2015" #to when 
    
    for(j in 1:length(wkn)){ 
    tg<-htmlParse(paste0("http://www.ariva.de/",wkn[j],"/historische_kurse")) #parse the historical webpage 
    atbts<-xpathSApply(tg,'//div[@class="content left abstand"]/input') #extract attributes for that stock so we can download it 
    
    secu<-sapply(atbts, function(x) xmlAttrs(x)['value'])[1] #security id (only relevant for downloading) 
    boerseid<-sapply(atbts, function(x) xmlAttrs(x)['value'])[2] #boerse id (only relevant for downloading) 
    
    #download, read and assign the values 
    assign(wkn[j],read.csv(paste0("http://www.ariva.de/quote/historic/historic.csv?secu=",secu,"&boerse_id=",boerseid, 
           "&clean_split=1&clean_payout=0&clean_bezug=1&min_time=",date.F,"&max_time=",date.T,"&trenner=%3B&go=Download"), 
           sep=";")) 
    write.csv(get(wkn[j]),paste0(wkn[j],".csv")) 
    } 
    
    +0

    嗨,你能解釋一下(或指出正確的方向)你如何計算出這個URL嗎?我正在用Firefox Inspector查看ariva頁面,但沒有提示這些參數,是嗎? –

    +1

    我使用了Chrome瀏覽器,並通過右鍵單擊使用「檢查元素」。通過轉到「檢查元素」內的「網絡」選項卡,我可以看到「請求URL」和查詢參數。 – nadizan