【发布时间】:2015-03-12 09:26:57
【问题描述】:
我正在尝试从网页中提取数据。出于这个原因,我想用 3 个变量创建一个函数: 1) 要查看的网页向量 2) 输出文件中的列名向量 3) 输出文件中每一列的 html 代码中的标记向量
parser <- function(fileUrl, itemName, itemMark)
{
library(XML)
total_result <- data.frame()
for (file in fileUrl) {
temp <- data.frame(1)
itemTemp <- c()
doc <-htmlTreeParse(file,useInternal=TRUE)
for (i in 1:length(itemName)){
itemTemp <- xpathSApply(doc,itemMark[[i]], xmlValue)
temp <- data.frame(temp,itemTemp, check.rows=FALSE)
}
total_result <- rbind(total_result, temp)
}
total_result[,1]<-NULL
names(total_result) <- itemName
total_result
}
它确实适用于网页上出现频率相同的项目。但它不适用于其他情况。我有这样的错误
data.frame 中的错误(temp,itemTemp,check.rows = FALSE):参数 暗示不同的行数:100, 0
我确实明白,对于 data.frame 函数,我需要参数具有相同的行数,但我不知道如何使我的解析器工作。 你能帮帮我吗?
重现错误:
url <- c("http://www.ebay.com/sch/Cell-Phones-Smartphones-/9355/i.html?LH_BIN=1&_from=R40&LH_ItemCondition=1000&_nkw=(iphone,%20htc,%20samsung,%20lg,%20nokia,%20sony)&_dcat=9355&rt=nc&_pppn=r1&Carrier=Unlocked|!","http://www.ebay.com/sch/Cell-Phones-Smartphones-/9355/i.html?LH_BIN=1&_from=R40&LH_ItemCondition=1000&_dcat=9355&Carrier=Unlocked%7C%21&_nkw=%28iphone%2C+htc%2C+samsung%2C+lg%2C+nokia%2C+sony%29&_pgn=2&_skc=50&rt=nc")
marks <- c("//span[@class='cbx']","//span[@class='cnt']")
names < c("1a","2a")
parser(url,names,marks)
【问题讨论】:
-
你能让问题重现吗?
-
我添加了重现错误的代码