0

I'm new to web-scraping. Was using the SelectorGadget and rvest to take the data from "https://fbref.com/en/comps/9/defense/Premier-League-Stats". I'm unable to take the data from the players table.

The code I'm using

url <- "https://fbref.com/en/comps/9/defense/Premier-League-Stats"
def_stats <- url %>% read_html() %>% html_nodes("#stats_defense") %>% html_table()

But this is returning an empty list. If I use url %>% read_html() %>% html_table() it just returns the Teams stats table.

I tried using various css selectors, "#stats_defense", #div_stats_defense, and even selecting the individual columns of the table. But it's returning an empty list.

1

1 Answer 1

0

I have been able to scrape the table with the following code :

library(RSelenium)

shell('docker run -d -p 4446:4444 selenium/standalone-firefox')
remDr <- remoteDriver(remoteServerAddr = "localhost", port = 4446L, browserName = "firefox")
remDr$open()
remDr$navigate("https://fbref.com/en/comps/9/defense/Premier-League-Stats")

web_Obj_Table <- remDr$findElement("id", "all_stats_squads_defense")
text_Table <- web_Obj_Table$getElementText()[[1]]
strsplit(text_Table, "\n")

[[1]]
 [1] "Squad Defensive Actions 2022-2023 Premier League"                                                                
 [2] "Share & Export"                                                                                                  
 [3] "GlossaryToggle Per90 Stats"                                                                                      
 [4] "Squad Stats Opponent Stats"                                                                                      
 [5] "Tackles Challenges Blocks"                                                                                       
 [6] "Squad # Pl 90s Tkl TklW Def 3rd Mid 3rd Att 3rd Tkl Att Tkl% Lost Blocks Sh Pass Int Tkl+Int Clr Err"            
 [7] "Arsenal 26 30.0 448 275 193 164 91 193 368 52.4 175 283 68 215 195 643 486 18"                                   
 [8] "Aston Villa 26 31.0 534 285 263 207 64 265 520 51.0 255 373 103 270 269 803 567 12"                              
 [9] "Bournemouth 31 31.0 502 307 254 201 47 206 471 43.7 265 417 147 270 289 791 848 9"                               
[10] "Brentford 25 31.0 490 286 225 196 69 215 505 42.6 290 376 141 235 288 778 695 11"                                
[11] "Brighton 26 29.0 494 271 221 191 82 207 407 50.9 200 296 73 223 242 736 333 11"                                  
[12] "Chelsea 32 31.0 628 374 310 223 95 280 544 51.5 264 379 86 293 294 922 560 14"                                   
[13] "Crystal Palace 26 31.0 579 354 299 210 70 231 440 52.5 209 392 103 289 311 890 683 5"                            
[14] "Everton 28 31.0 589 356 307 211 71 253 483 52.4 230 421 148 273 298 887 701 17"                                  
[15] "Fulham 29 30.0 483 272 226 173 84 224 427 52.5 203 356 96 260 269 752 581 9"                                     
[16] "Leeds United 28 30.0 684 399 309 279 96 332 678 49.0 346 421 101 320 316 1000 563 12"                            
[17] "Leicester City 28 31.0 560 337 266 239 55 257 554 46.4 297 420 134 286 309 869 607 8"                            
[18] "Liverpool 27 29.0 440 279 189 185 66 201 477 42.1 276 282 64 218 264 704 434 13"                                 
[19] "Manchester City 22 30.0 382 220 146 160 76 172 359 47.9 187 278 74 204 180 562 298 6"                            
[20] "Manchester Utd 26 29.0 505 294 279 170 56 232 442 52.5 210 385 119 266 267 772 625 6"                            
[21] "Newcastle Utd 26 30.0 493 300 218 201 74 251 499 50.3 248 365 94 271 267 760 564 9"                              
[22] "Nott'ham Forest 33 30.0 500 296 276 163 61 228 439 51.9 211 391 125 266 258 758 729 5"                           
[23] "Southampton 31 31.0 590 342 297 243 50 244 475 51.4 231 426 115 311 343 933 658 6"                               
[24] "Tottenham 27 31.0 492 283 256 169 67 212 442 48.0 230 344 116 228 273 765 604 15"                                
[25] "West Ham 25 29.0 469 250 222 175 72 201 413 48.7 212 322 106 216 307 776 616 7"                                  
[26] "Wolves 31 31.0 570 320 317 189 64 241 496 48.6 255 394 139 255 231 801 672 10"                                   
[27] "Totals may not be complete for all senior-level play, see coverage note."                                        
[28] "Expected Goals (xG explained) and other Advanced Data provided by Opta, and is available for these competitions."
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.