使用 XML 包将 html 表抓取到 R 数据框 [英] Scraping html tables into R data frames using the XML package

查看:36
本文介绍了使用 XML 包将 html 表抓取到 R 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 XML 包抓取 html 表格?

How do I scrape html tables using the XML package?

巴西足球队上的这个维基百科页面为例.我想在 R 中阅读它并获得巴西与国际足联认可球队进行的所有比赛的列表"表作为数据框架.我该怎么做?

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?

推荐答案

...或更短的尝试:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆