如何将 html 表导入 R?

休伯特

我只是想将一个 html 表导入 R。我尝试了其他人的错误并广泛查看了给出的解决方案,尽管我的问题仍然出现。我知道这很容易,但是由于某种原因,它对我不起作用。我收到以下错误:
错误:无法加载外部实体“http://www.formula1.com/en/results.html/2020/races/1045/austria/race-result.html”(对于第一个功能)
(函数(类,fdef,mtable)中的错误:无法为签名“NULL”的函数“readHTMLTable”找到继承的方法(第二个)

正如我所提到的,我已经查看了其他人的问题并尝试使用他们的解决方案来解决我的问题,尽管仍然出现相同的问题。也许这可能是由于我的设置(我不确定),但是有人可以帮助我吗?

library(XML)
library(rjson)
library(RCurl)
library(htmltools)

url<- "http://www.formula1.com/en/results.html/2020/races/1045/austria/race-result.html"
austria = readHTMLTable(url, which=1)

tabs <- getURL("http://www.formula1.com/en/results.html/2020/races/1045/austria/race-result.html")
tabs1 <- readHTMLTable(tabs, which=1)

罗纳克·沙阿

rvest您可以使用html_table从页面获取表格:

library(rvest)

url <- 'https://www.formula1.com/en/results.html/2020/races/1045/austria/race-result.html'
url %>%
  read_html %>%
  html_table() %>%
  .[[1]] -> df

df

#      Pos No                                                           Driver
#1  NA   1 77    Valtteri\n                    Bottas\n                    BOT
#2  NA   2 16    Charles\n                    Leclerc\n                    LEC
#3  NA   3  4       Lando\n                    Norris\n                    NOR
#4  NA   4 44     Lewis\n                    Hamilton\n                    HAM
#5  NA   5 55       Carlos\n                    Sainz\n                    SAI
#6  NA   6 11       Sergio\n                    Perez\n  
#...
#...                  

#                         Car Laps Time/Retired PTS   
#1                   Mercedes   71  1:30:55.739  25 NA
#2                    Ferrari   71      +2.700s  18 NA
#3            McLaren Renault   71      +5.491s  16 NA
#4                   Mercedes   71      +5.689s  12 NA
#5            McLaren Renault   71      +8.903s  10 NA
#6  Racing Point BWT Mercedes   71     +15.092s   8 NA
#...
#...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章