使用XML包将html表格刮到R数据框中 [英] Scraping html tables into R data frames using the XML package
问题描述
如何使用XML包对html表格进行刮取?
例如,在巴西足球队。我想在R中读取它,并将巴西队与国际足联认可的球队对阵的所有比赛名单列为data.frame。如何才能做到这一点?
...或者更短的尝试:
<
library(rlist)
theurl< - getURL(https://en.wikipedia。 org / wiki / Brazil_national_football_team,。opts = list(ssl.verifypeer = FALSE))
tables< - readHTMLTable(theurl)
tables< --list.clean(tables,fun = is.null ,recursive = FALSE)
n.rows< - unlist(lapply(tables,function(t)dim(t)[1]))
所选表格是页面上最长的表格
表格[ which.max(n.rows)]]
How do I scrape html tables using the XML package?
Take, for example, this wikipedia page on the Brazilian soccer team. I would like to read it in R and get the "list of all matches Brazil have played against FIFA recognised teams" table as a data.frame. How can I do this?
…or a shorter try:
library(XML)
library(RCurl)
library(rlist)
theurl <- getURL("https://en.wikipedia.org/wiki/Brazil_national_football_team",.opts = list(ssl.verifypeer = FALSE) )
tables <- readHTMLTable(theurl)
tables <- list.clean(tables, fun = is.null, recursive = FALSE)
n.rows <- unlist(lapply(tables, function(t) dim(t)[1]))
the picked table is the longest one on the page
tables[[which.max(n.rows)]]
这篇关于使用XML包将html表格刮到R数据框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!