一、网页结构解析
数据来源:
通过“审查元素”选项,查看标签信息:
二、构造数据查询函数
> 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)
+ }
三、输入参数,查询数据
另外,R的quantmod包内的函数也可以达到同样的效果。
(End)