2016-09-27 52 views
0

我试图将XMLInternalElementNode解析为数据框。 我已阅读How to parse XML to R data frameHow to get table data from html table in xml但这些解决方案都不适用于我的案例。解析XML节点以获取R表中的表数据

下面我的代码不给我一个表:

web=getURL("http://www.tocom.or.jp/market/kobetu/rubber.html", header=FALSE, httpheader = c(Accept="text/html"), verbose = TRUE) 
    doc=htmlParse(web, asText=TRUE, encoding="Windows-1252") 
    tableNodes = getNodeSet(doc, "//table") 

    #this gives me error 
    xmlParse(tableNodes[[2]]) 
    Error in as.vector(x, "character") : 
    cannot coerce type 'externalptr' to vector of type 'character' 

    #This does not return me the table neither: 
    xpathSApply(tableNodes[[2]], path = '//table//tr') 

所以我应该如何从这个网站检索表?

+1

在调用'tableNodes = getNodeSet(doc,“// table”)''之后,您已经拥有了所有表格。但是,即使在那之后,似乎'readHTMLTable()'由于某种原因无法解析这些内容,因此您应该尝试使用@ Floo0的答案。 – hrbrmstr

回答

2

什么:

library(rvest) 
doc <- read_html("http://www.tocom.or.jp/market/kobetu/rubber.html") 
doc %>% html_table(fill=TRUE) 

,让你的所有表的列表。

+0

谢谢。有用! – user6885562