数据来源网站:html
经过“审查元素”选项查看每个变量的html结构: node
> library(openxlsx) > stockcode<-read.xlsx("C:/Users/steph/Desktop/stock/stockcode.xlsx",1,colNames=F) #文件包含股票代码信息 > codes<-as.vector(stockcode[[1]]) #数据类型转换 > EPS<-vector(length=length(codes)) #每股收益 > NAV<-vector(length=length(codes)) #每股净资产 > ROE<-vector(length=length(codes)) #净资产收益率 > ND<-vector(length=length(codes)) #每股未分配利润 > CR<-vector(length=length(codes)) #每股资本公积 > OCFPS<-vector(length=length(codes)) #每股经营现金流 > NP<-vector(length=length(codes)) #净利润(万元) > OI<-vector(length=length(codes)) #营业收入(万元) > ROI<-vector(length=length(codes)) #投资收益(万元) > FE<-vector(length=length(codes)) #财务费用(万元)
> library(rvest) > for(n in 1:length(codes)){ + i<-codes[n] + url<-paste("http://finance.ifeng.com/app/hq/stock/",i,"/",sep="") + res1<-tryCatch({read_html(url)%>%html_nodes(".tabInfor06")%>%html_nodes("li")%>%.[1]%>%html_nodes("td")%>%.[2]%>%html_text%>%as.numeric()},error=function(e){"NA"}) + EPS[n]<-res1 + res2<-tryCatch({read_html(url)%>%html_nodes(".tabInfor06")%>%html_nodes("li")%>%.[2]%>%html_nodes("td")%>%.[2]%>%html_text%>%as.numeric()},error=function(e){"NA"}) + NAV[n]<-res2 + res3<-tryCatch({read_html(url)%>%html_nodes(".tabInfor06")%>%html_nodes("li")%>%.[3]%>%html_nodes("td")%>%.[2]%>%html_text%>%as.numeric()},error=function(e){"NA"}) + ROE[n]<-res3 + res4<-tryCatch({read_html(url)%>%html_nodes(".tabInfor06")%>%html_nodes("li")%>%.[4]%>%html_nodes("td")%>%.[2]%>%html_text%>%as.numeric()},error=function(e){"NA"}) + ND[n]<-res4 + res5<-tryCatch({read_html(url)%>%html_nodes(".tabInfor06")%>%html_nodes("li")%>%.[5]%>%html_nodes("td")%>%.[2]%>%html_text%>%as.numeric()},error=function(e){"NA"}) + CR[n]<-res5 + res6<-tryCatch({read_html(url)%>%html_nodes(".tabInfor06")%>%html_nodes("li")%>%.[6]%>%html_nodes("td")%>%.[2]%>%html_text%>%as.numeric()},error=function(e){"NA"}) + OCFPS[n]<-res6 + res7<-tryCatch({read_html(url)%>%html_nodes(".tabInfor06")%>%html_nodes("li")%>%.[7]%>%html_nodes("td")%>%.[2]%>%html_text%>%as.numeric()},error=function(e){"NA"}) + NP[n]<-res7 + res8<-tryCatch({read_html(url)%>%html_nodes(".tabInfor06")%>%html_nodes("li")%>%.[8]%>%html_nodes("td")%>%.[2]%>%html_text%>%as.numeric()},error=function(e){"NA"}) + OI[n]<-res8 + res9<-tryCatch({read_html(url)%>%html_nodes(".tabInfor06")%>%html_nodes("li")%>%.[9]%>%html_nodes("td")%>%.[2]%>%html_text%>%as.numeric()},error=function(e){"NA"}) + ROI[n]<-res9 + res10<-tryCatch({read_html(url)%>%html_nodes(".tabInfor06")%>%html_nodes("li")%>%.[10]%>%html_nodes("td")%>%.[2]%>%html_text%>%as.numeric()},error=function(e){"NA"}) + FE[n]<-res10 + next + }
> FS<-data.frame(EPS,NAV,ROE,ND,CR,OCFPS,NP,OI,ROI,FE) #将抓取的数据构造数据框 > companydetail<-read.xlsx("C:/Users/steph/Desktop/stock/companydetail.xlsx",1,colNames=F) #导入股票其它信息 > companyfinacedetail<-cbind(companydetail,FS) #合并数据框 > names(companyfinacedetail)<-c("股票代码","公司名称","所属行业","总市值(万元)","流通市值(万元)","每股收益","每股净资产","净资产收益率%","每股未分配利润","每股资本公积","每股经营现金流","净利润(万元)","营业收入(万元)","投资收益(万元)","财务费用(万元)") #修改变量名称 > head(companyfinacedetail,n=10) #提取前10间公司财务信息
结果以下:es6
(End)app