【发布时间】:2012-02-12 15:23:49
【问题描述】:
我想从 metal-archives.com 下载表格,正好来自 http://www.metal-archives.com/artist/rip,但是有一个大问题。此表由 javascript 生成。事实上,我不知道在这种情况下该怎么办。
有没有可能用 R 和 XML 包来解析这个网站?
【问题讨论】:
标签: javascript r web-scraping
我想从 metal-archives.com 下载表格,正好来自 http://www.metal-archives.com/artist/rip,但是有一个大问题。此表由 javascript 生成。事实上,我不知道在这种情况下该怎么办。
有没有可能用 R 和 XML 包来解析这个网站?
【问题讨论】:
标签: javascript r web-scraping
这里是 JSON 格式的所有信息
【讨论】:
感谢用户bubmu 我实现了我想要的。下面是代码,解决了我的问题。
a<-1:8
b<-200*a
x<-paste("http://www.metal-archives.com/artist/ajax-rip?iDisplayStart=",b,"&sEcho=",a,sep="")
x<-c(x,"http://www.metal-archives.com/artist/ajax-rip?iDisplayStart=1700&sEcho=9")
JSONparse<-function(x){
library(XML)
doc<-htmlParse(x)
str<-xpathApply(doc,'//p',xmlValue)[[1]][1]
x1<-strsplit(str,'\\[')
x1<-x1[[1]][-1]
x1<-x1[-1]
x2<-strsplit(x1,'\\",')
x3<-lapply(x2, function(y) {
y<-gsub('\\t','',y)
y<-gsub('\\n','',y)
y<-gsub('\\r','',y)
y<-gsub('\\\"','',y)
y<-gsub('\\]}','',y)
y<-gsub('\\],','',y)
y<-as.data.frame(t(y))
y})
allinall<-do.call('rbind',x3)
colnames(allinall)<-c("Artist","Country","Band","When","Why")
allinall
}
metallum<-lapply(x,JSONparse)
metallum<-do.call('rbind',metallum)
但它仅适用于本网站。当然更好的是 RJSONIO 或 rjson 包。
【讨论】: