R:構造函數抓取股票歷史數據

1、網頁結構解析php

數據來源:html

http://biz.finance.sina.com.cn/stock/flash_hq/kline_data.php?symbol=sh601369&end_date=20161125&begin_date=20161110node

輸入圖片說明

經過「審查元素」選項,查看標籤信息:web

輸入圖片說明


2、構造數據查詢函數函數

> get_stockprice<-function(code,startday,endday){
        
+ library(rvest)

+ i <-"http://biz.finance.sina.com.cn/stock/flash_hq/kline_data.php?
symbol=sh"

+ url<-paste(i,code,"&end_date=",endday,"&begin_date=,"startday",sep="")
        
+ web<-read_html(url)
        
+ res<-do.call(rbind,data<-web%>%html_nodes("content")%>%html_attrs())%>%as.data.frame
        
+ res2<-res[,1:5]
        
+ names(res2)<-c("日期","開盤價","最高價","收盤價","最低價","成交量")
        
+ print(res2)
    
+ }

3、輸入參數,查詢數據url

輸入圖片說明

另外,R的quantmod包內的函數也能夠達到一樣的效果。code

(End)htm

相關文章
相關標籤/搜索