Hubert
Hubert

Reputation: 23

How do I import a html table into R?

I'm just trying to import a html table into R. I have tried other people's mistakes and looked extensively at the solutions given, though my problems still arise. I understand this is easy, however for some reason it just isn't working for me. I get the following errors:
Error: failed to load external entity "http://www.formula1.com/en/results.html/2020/races/1045/austria/race-result.html" (for the first function)
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘readHTMLTable’ for signature ‘"NULL"’ (for the second)

As I have mentioned, I have looked at other people's problems and tried solving mine using their solutions, though the same problems still arise. Perhaps this could be due to my settings(I'm not sure), however can anyone help me please?

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)

Upvotes: 2

Views: 1638

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388947

In rvest you can use html_table to get the table from the page :

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
#...
#...

Upvotes: 4

Related Questions