1、網頁結構解析php
數據來源:html
經過「審查元素」選項,查看標籤信息: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