[問題類型]:
網頁爬蟲,已找出xpath,但無法製作成data.frame
[軟體熟悉度]:
中新手
[問題敘述]:
網頁爬蟲,想把wiki的球隊資訊爬下來
[程式範例]:
library(magrittr)
library(rvest)
URLteam <-
"https://zh.wikipedia.org/wiki/%E7%BE%8E%E5%9C%8B%E8%81%B7%E6%A5%AD%E6%A3%92%E7%90%83%E5%A4%A7%E8%81%AF%E7%9B%9F"
#網頁
xpathTeam <- "//table[@class='navbox wikitable']/tbody/tr/td[1]"#球隊隊名的
xpath
docTeam <- read_html(URLteam, encoding="UTF-8")#將網頁讀進R
rankTeam<- docTeam %>%
html_nodes(.,xpath = xpathTeam) %>%
html_text %>%
iconv(from = "UTF-8", to = "UTF-8")
stockTmp <- data.frame(team=docTeam)
以下是我噴錯的error
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors =
stringsAsFactors) :
cannot coerce class "c("xml_document", "xml_node")" to a data.frame
問題1: 不知道怎麼了解自己xpath有沒有抓對
問題2:data.frame是不是有設定錯誤?