我使用以下代码从网站(http://q.stock.sohu.com/cn/000002/lshq.shtml)获取信息。但我不知道如何获得“日期,打开,关闭,高,低”的数据框架。任何帮助都将不胜感激。
thepage = readLines('http://q.stock.sohu.com/hisHq?code=cn_000002&start=20131120&end=20140318&stat=1&order=D&period=d&callback=historySearchHandler&rt=jsonp&r=0.8740235545448934&0.28161772061461654')怎样才能得到数据框架?
发布于 2014-03-20 02:39:26
我不知道返回JSON的哪些部分是您需要的实际值,但我假设它们是hq记录的组件。这应该是可行的:
library(RJSONIO)
library(RCurl)
# get the raw data
dat.json.raw <- getURL("http://q.stock.sohu.com/hisHq?code=cn_000002&start=20131120&end=20140318&stat=1&order=D&period=d&callback=historySearchHandler&rt=jsonp&r=0.8740235545448934&0.28161772061461654%27")
tt <- textConnection(dat.json.raw)
dat.json <- readLines(tt)
close(tt)
# remove callback
dat.json <- gsub("^historySearchHandler\\(", "", dat.json)
dat.json <- gsub("\\)$", "", dat.json)
# convert to R structure
dat.l <- fromJSON(dat.json)
# get the meaty part of the data into a data.frame
dat <- data.frame(t(sapply(dat.l[[1]]$hq, unlist)), stringsAsFactors=FALSE)
dat$X1 <- as.Date(dat$X1)
dat$X2 <- as.numeric(dat$X2)
dat$X3 <- as.numeric(dat$X3)
dat$X4 <- as.numeric(dat$X4)
str(dat)
## 'data.frame': 79 obs. of 10 variables:
## $ X1 : Date, format: "2014-03-18" "2014-03-17" "2014-03-14" ...
## $ X2 : num 7.76 7.6 7.68 7.58 7.48 7.19 7.22 7.34 6.76 6.92 ...
## $ X3 : num 7.6 7.76 7.53 7.71 7.6 7.5 7.15 7.27 7.32 6.76 ...
## $ X4 : num -0.16 0.23 -0.18 0.11 0.1 0.35 -0.12 -0.05 0.56 -0.16 ...
## $ X5 : chr "-2.06%" "3.05%" "-2.33%" "1.45%" ...
## $ X6 : chr "7.55" "7.59" "7.50" "7.53" ...
## $ X7 : chr "7.76" "7.80" "7.81" "7.85" ...
## $ X8 : chr "843900" "1177079" "1303110" "1492359" ...
## $ X9 : chr "64268.06" "90829.30" "99621.34" "114990.40" ...
## $ X10: chr "0.87%" "1.22%" "1.35%" "1.54%" ...
head(dat)
## X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
## 1 2014-03-18 7.76 7.60 -0.16 -2.06% 7.55 7.76 843900 64268.06 0.87%
## 2 2014-03-17 7.60 7.76 0.23 3.05% 7.59 7.80 1177079 90829.30 1.22%
## 3 2014-03-14 7.68 7.53 -0.18 -2.33% 7.50 7.81 1303110 99621.34 1.35%
## 4 2014-03-13 7.58 7.71 0.11 1.45% 7.53 7.85 1492359 114990.40 1.54%
## 5 2014-03-12 7.48 7.60 0.10 1.33% 7.42 7.85 2089873 160315.88 2.16%
## 6 2014-03-11 7.19 7.50 0.35 4.90% 7.15 7.59 1892488 141250.94 1.96%您需要修复其他一些列(因为我不知道您到底需要什么)。
对于不喜欢fromJSON调用返回的警告的人,只需用paste:dat.json <- paste(readLines(tt), collapse="")包装readLines即可。这是不必要的(警告是无害的),所以我通常不麻烦额外的步骤。
发布于 2014-03-20 01:04:54
似乎你在试图刮一个在JSON中显示数据的网站。
为此,除了需要执行“常规步骤”以抓取网站之外,还需要处理解析和操作JSON数据:
常用方法
如果您有一个易于抓取表的HTML,那么应该可以这样做:
require("XML")
x <- readHTMLTable(
doc="swww.someurl.com"
)否则,您肯定需要使用一些XPath来访问您感兴趣的节点。
这通常涉及通过htmlTreeParse()解析HTML代码,并通过getNodeSet()等访问相应的节点:
x <- htmlTreeParse(
file="swww.someurl.com",
isURL=TRUE,
useInternalNodes=TRUE
)
res <- getNodeSet(x, <your-xpath-statement>)包含JSON数据的方法
解析HTML代码:
x <- htmlTreeParse(
file="http://q.stock.sohu.com/hisHq?code=cn_000002&start=20131120&end=20140318&stat=1&order=D&period=d&callback=historySearchHandler&rt=jsonp&r=0.8740235545448934&0.28161772061461654",
isURL=TRUE,
useInternalNodes=TRUE
)检索实际的JSON数据:
json <- getNodeSet(x, "//body/p")
json <- xmlValue(json[[1]])去掉非JSON组件:
json <- gsub("historySearchHandler\\(", "", json, perl=TRUE)
json <- gsub("\\)$", "", json, perl=TRUE)解析JSON数据:
require("jsonlite")
fromJSON(json, simplifyVector=FALSE)
[[1]]
[[1]]$status
[1] 0
[[1]]$hq
[[1]]$hq[[1]]
[[1]]$hq[[1]][[1]]
[1] "2014-03-18"
[[1]]$hq[[1]][[2]]
[1] "7.76"
[...]现在,您需要将其纳入更多的data.frame-like顺序(想到的方法是do.call()、rbind()、cbind)。
编码
迟早(就像我们在这个例子中看到的那样),您将面临编码问题(比如"ÀÛ¼Æ:")。
您可以在解析HTML代码(htmlTreeParse()中的参数htmlTreeParse())时直接处理不同的编码,也可以通过“事后”通过Encoding修改字符串的编码。不过,我没能让你的价值观完全正确。编码问题可能会很痛苦。
一般建议
我建议你在将来选择基于英语的例子(在这种情况下是一个基于英语的网站),因为否则你将极大地限制能够帮助你的人的数量。
https://stackoverflow.com/questions/22521107
复制相似问题