[问题类型]:
网页爬虫,已找出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是不是有设定错误?