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

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

问题描述

如何使用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屋!

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