【问题标题】:R - Extracting Tables From Websites Using XML PackageR - 使用 XML 包从网站中提取表格
【发布时间】:2014-06-13 18:04:55
【问题描述】:

我正在尝试为我自己的工作复制先前答案Scraping html tables into R data frames using the XML package 中使用的方法,但无法获取要提取的数据。我正在使用的网站是: http://www.footballfanalytics.com/articles/football/euro_super_league_table.html

我只想提取每个团队名称及其当前评分的表格。我的代码如下:

library(XML)
theurl <-  "http://www.footballfanalytics.com/articles/football/euro_super_league_table.html"
tables <- readHTMLTable(theurl)
n.rows <- unlist(lapply(tables, function(t) dim(t)[1]))
tables[[which.max(n.rows)]]

这会产生错误消息

Error in tables[[which.max(n.rows)]] : 
attempt to select less than one element

有人可以提出解决方案吗?这个特定站点中是否有某些东西导致它不起作用?或者有没有更好的替代方法我可以尝试?谢谢

【问题讨论】:

    标签: html xml r


    【解决方案1】:

    似乎数据是通过 javascript 加载的。试试:

    library(XML)
    theurl <- "http://www.footballfanalytics.com/xml/esl/esl.xml"
    doc <- xmlParse(theurl)
    cbind(team = xpathSApply(doc, "/StatsData/Teams/Team/Name", xmlValue),
          points = xpathSApply(doc, "/StatsData/Teams/Team/Points", xmlValue))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-29
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多