Last active
December 11, 2015 14:59
-
-
Save yanping/4618297 to your computer and use it in GitHub Desktop.
从和讯网读取上市公司财务数据
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# readHexun.r | |
# 从和讯读取财务数据 | |
readHexun <- function(stockid, year, accountdate ){ | |
if(accountdate == 1){ | |
ad <- paste(year,".03.15",sep="") | |
}else if(accountdate == 2){ | |
ad <- paste(year,".06.30",sep="") | |
}else if(accountdate == 3){ | |
ad <- paste(year,".09.30",sep="") | |
}else if(accountdate == 4){ | |
ad <- paste(year,".12.31",sep="") | |
}else{ | |
stop("会计年度输入有误!") | |
} | |
stockid <- as.character(stockid) | |
while(nchar(stockid) < 6){ | |
stockid <- paste("0",stockid,sep="") | |
} | |
if(nchar(stockid) > 6){ | |
warning(paste("invalid stock code: ",stockid,sep="")) | |
} | |
require(XML) | |
require(RCurl) | |
require(stringr) | |
address <- "http://stockdata.stock.hexun.com/2008/zcfz.aspx?stockid=" | |
url <- paste(address,stockid, "&accountdate=", ad,sep="") | |
doc <- getURL(url) | |
txt <- htmlParse(doc,encoding = 'GB2312') | |
data <- sapply(getNodeSet(txt, "//table[@class='web2']//div[@class='tishi']"), xmlValue) | |
m <- matrix(data, nrow=2,byrow = F) | |
if(dim(m)[1]!= 2 | dim(m)[2]!= 69 ){ | |
stop("数据有问题!请检查...") | |
}else{ | |
df <- as.data.frame(t(as.numeric(gsub(",","",m[2,2:68])))) | |
df$date <- m[2,1] | |
df$remark <- m[2,69] | |
return(df) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment