我有以下代码,旨在访问网站,获取一些数据并将其放入data.frame
create_links <- function(keyword, distance) {
data.frame <- data.frame(character(), character())
postcode <- c(3511, 4000, 5000)
var_website1 <- "http://www.marktplaats.nl/z.html?query="
var_website2 <- "&postcode="
var_website3 <- "&distance="
for (i in 1:length(postcode)) {
website <- paste0(var_website1, keyword, var_website2, postcode[i], var_website3, distance)
html <- read_html(website)
number <- html_nodes(html, "h1 span")
number <- as.character(number)
website <- as.character(website)
data.frame <- rbind(data.frame, number, website)
}
data.frame
}
当我打电话给公式时: 库(rvest) create_links(“bureaustoel”,10000)
所有似乎都有效,但我只是将数值放在data.frame中有一点麻烦。我收到以下错误:
1: In `[<-.factor`(`*tmp*`, ri, value = "<span>20.474 resultaten voor 'bureaustoel'</span>") :
invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, ri, value = "http://www.marktplaats.nl/z.html?query=bureaustoel&postcode=4000&distance=10000") :
invalid factor level, NA generated
3: In `[<-.factor`(`*tmp*`, ri, value = "<span>20.474 resultaten voor 'bureaustoel'</span>") :
invalid factor level, NA generated
我不知道为什么我会得到错误(导致我在将值转换为data.frame之前将值转换为字符?
有什么想法吗?