Skip to content

Instantly share code, notes, and snippets.

@tonyelhabr
Created January 1, 2025 20:07
Show Gist options
  • Save tonyelhabr/5ce6420a85bc9f5f53b49e8b393fe10a to your computer and use it in GitHub Desktop.
Save tonyelhabr/5ce6420a85bc9f5f53b49e8b393fe10a to your computer and use it in GitHub Desktop.
How to get fotmob league stats with R

General Guide

  1. In your browser, go the URL where you can find the data that you want. For example, https://www.fotmob.com/leagues/126/stats/premier-division?season=2024
  2. Use the browser's "Inspect" feature and "Network" tab to find the right GET request. You should be able to tell which request is the right one by looking to see if there is a detailed JSON response in the "Response" tab. image
  3. Right click on the request and copy the CURL command. image
  4. Go to https://curlconverter.com, copy the CURL command into the "curl command" box at the top of the page with "R > httr2" (or "R > httr") selected. image
  5. Copy the R commands from the website into a RStudio and work on parsing out the content that you want. For example
library(httr2)

resp <- # all the stuff from curlconverter

# My parsing code
cont <- resp_body_json(resp)
teams <- cont$stats$teams

Guide for League Team/Player Stats

In this specific example, you may actually just be ordered in one stat--for example, team goals per match. To extract this, it's easier to identify the data URL in the CURL response, navigate to that URL in a separate browser tab to identify the elements that you'll want, then parse things with code. image image

library(httr2)
library(dplyr)
library(janitor)

resp_stat <- request('https://data.fotmob.com/stats/126/season/22578/goals_team_match.json') |> 
  req_perform()
cont_stat <- resp_body_json(resp_stat)
cont_stat$TopLists[[1]]$StatList |> 
  bind_rows() |> 
  clean_names()
#> # A tibble: 10 × 11
#>    participant_name   particiant_id team_id team_color stat_value sub_stat_value
#>    <chr>                      <int>   <int> <chr>           <dbl>          <dbl>
#>  1 St. Patrick's Ath…             0    1854 #EC1B23           1.4             51
#>  2 Shamrock Rovers                0    4131 #088020           1.4             50
#>  3 Derry City                     0    8338 #D80000           1.3             48
#>  4 Waterford FC                   0    6042 #185890           1.2             43
#>  5 Drogheda United                0    8339 #7B0047           1.1             41
#>  6 Shelbourne                     0    5751 #D01810           1.1             40
#>  7 Sligo Rovers                   0    6361 #B81010           1.1             40
#>  8 Bohemian FC                    0    4594 #b00017           1.1             39
#>  9 Galway United FC               0  520517 #620531           0.9             33
#> 10 Dundalk                        0    1853 #202020           0.6             23
#> # ℹ 5 more variables: minutes_played <int>, matches_played <int>,
#> #   stat_value_count <int>, rank <int>, participant_country_code <chr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment