Scrape FPL Website and Optimise Team#

[ ]:
import penaltyblog as pb

Get the current active gameweek number#

[2]:
current_gameweek = pb.fpl.get_current_gameweek()
current_gameweek
[2]:
16

Get the weekly events#

This contains information on who the most captainied player was for each week week, what chips were played, what the highest score was etc

[3]:
info = pb.fpl.get_gameweek_info()
info.head()
[3]:
id name deadline_time release_time average_entry_score finished data_checked highest_scoring_entry deadline_time_epoch deadline_time_game_offset ... ranked_count overrides chip_plays most_selected most_transferred_in top_element top_element_info transfers_made most_captained most_vice_captained
0 1 Gameweek 1 2024-08-16 17:30:00+00:00 None 57 True True 3546234.0 1723829400 0 ... 8597356 {'rules': {}, 'scoring': {}, 'element_types': ... [{'chip_name': 'bboost', 'num_played': 144974}... 401.0 27.0 328.0 {'id': 328, 'points': 14} 0 351.0 351.0
1 2 Gameweek 2 2024-08-24 10:00:00+00:00 None 69 True True 9442126.0 1724493600 0 ... 9543353 {'rules': {}, 'scoring': {}, 'element_types': ... [{'chip_name': 'bboost', 'num_played': 118782}... 401.0 594.0 177.0 {'id': 177, 'points': 20} 13364453 351.0 401.0
2 3 Gameweek 3 2024-08-31 10:00:00+00:00 None 64 True True 430195.0 1725098400 0 ... 9912161 {'rules': {}, 'scoring': {}, 'element_types': ... [{'chip_name': 'bboost', 'num_played': 87334},... 351.0 177.0 328.0 {'id': 328, 'points': 17} 23723836 351.0 328.0
3 4 Gameweek 4 2024-09-14 10:00:00+00:00 None 51 True True 3560750.0 1726308000 0 ... 10178753 {'rules': {}, 'scoring': {}, 'element_types': ... [{'chip_name': 'bboost', 'num_played': 83637},... 351.0 129.0 185.0 {'id': 185, 'points': 15} 25930392 351.0 328.0
4 5 Gameweek 5 2024-09-21 10:00:00+00:00 None 58 True True 1773336.0 1726912800 0 ... 10279610 {'rules': {}, 'scoring': {}, 'element_types': ... [{'chip_name': 'bboost', 'num_played': 66447},... 351.0 58.0 180.0 {'id': 180, 'points': 16} 13036058 351.0 351.0

5 rows × 29 columns

Get Player Name to ID mappings#

[4]:
mappings = pb.fpl.get_player_id_mappings()
mappings.head()
[4]:
first_name second_name web_name id
0 Fábio Ferreira Vieira Fábio Vieira 1
1 Gabriel Fernando de Jesus G.Jesus 2
2 Gabriel dos Santos Magalhães Gabriel 3
3 Kai Havertz Havertz 4
4 Karl Hein Hein 5

Get the player data#

This contains information on each player, e.g. total score, total minutes played, form, current price etc

[5]:
players = pb.fpl.get_player_data()
players.head()
[5]:
can_transact can_select chance_of_playing_next_round chance_of_playing_this_round code cost_change_event cost_change_event_fall cost_change_start cost_change_start_fall dreamteam_count ... points_per_game_rank_type selected_rank selected_rank_type starts_per_90 clean_sheets_per_90 position position_short team_id team_name team_name_short
0 True False 0.0 0.0 438098 0 0 -1 1 0 ... 310 636 278 0.00 0.00 Midfielder MID 1 Arsenal ARS
1 True True 100.0 100.0 205651 0 0 -2 2 0 ... 49 214 37 0.74 0.37 Forward FWD 1 Arsenal ARS
2 True True 75.0 75.0 226597 0 0 2 -2 2 ... 4 13 5 1.08 0.33 Defender DEF 1 Arsenal ARS
3 True True 100.0 100.0 219847 0 0 -1 1 1 ... 14 37 10 1.01 0.29 Forward FWD 1 Arsenal ARS
4 True False 0.0 0.0 463748 0 0 0 0 0 ... 58 558 68 0.00 0.00 Goalkeeper GKP 1 Arsenal ARS

5 rows × 96 columns

Let’s extract the data for Oleksandr Zinchenko from the data frame#

[6]:
zincs = mappings.query("web_name == 'Zinchenko'")
zincs_id = zincs["id"].iloc[0]
players.query("id == @zincs_id")
[6]:
can_transact can_select chance_of_playing_next_round chance_of_playing_this_round code cost_change_event cost_change_event_fall cost_change_start cost_change_start_fall dreamteam_count ... points_per_game_rank_type selected_rank selected_rank_type starts_per_90 clean_sheets_per_90 position position_short team_id team_name team_name_short
20 True True 75.0 100.0 206325 0 0 -2 2 0 ... 50 204 82 0.77 0.77 Defender DEF 1 Arsenal ARS

1 rows × 96 columns

Now extract the data about goalkeepers#

[7]:
players.query("position_short == 'GKP'")[
    ["id", "first_name", "second_name", "position", "total_points", "minutes"]
].sort_values("total_points", ascending=False).head()
[7]:
id first_name second_name position total_points minutes
466 383 André Onana Goalkeeper 65 1350
536 443 Matz Sels Goalkeeper 62 1350
11 15 David Raya Martin Goalkeeper 58 1350
276 235 Jordan Pickford Goalkeeper 58 1260
234 201 Dean Henderson Goalkeeper 53 1350

Get Zinchenko’s history by week for current season#

[8]:
history = pb.fpl.get_player_history(zincs_id)
history
[8]:
element fixture opponent_team total_points was_home kickoff_time team_h_score team_a_score round modified ... starts expected_goals expected_assists expected_goal_involvements expected_goals_conceded value transfers_balance selected transfers_in transfers_out
0 25 2 20 6 True 2024-08-17 14:00:00+00:00 2 0 1 False ... 1 0.00 0.04 0.04 0.32 5.0 0 230223 0 0
1 25 11 2 0 False 2024-08-24 16:30:00+00:00 0 2 2 False ... 0 0.00 0.00 0.00 0.00 5.0 23613 312806 46003 22390
2 25 21 5 1 True 2024-08-31 11:30:00+00:00 1 1 3 False ... 0 0.00 0.00 0.00 0.28 5.0 -56048 264308 16584 72632
3 25 39 18 0 False 2024-09-15 13:00:00+00:00 0 1 4 False ... 0 0.00 0.00 0.00 0.00 4.9 -45776 227563 9852 55628
4 25 47 13 0 False 2024-09-22 15:30:00+00:00 2 2 5 False ... 0 0.00 0.00 0.00 0.00 4.9 -44677 184385 1173 45850
5 25 51 11 0 True 2024-09-28 14:00:00+00:00 4 2 6 False ... 0 0.00 0.00 0.00 0.00 4.8 -40098 144882 164 40262
6 25 61 17 0 True 2024-10-05 14:00:00+00:00 3 1 7 False ... 0 0.00 0.00 0.00 0.00 4.8 -23935 121540 84 24019
7 25 71 3 0 False 2024-10-19 16:30:00+00:00 2 0 8 False ... 0 0.00 0.00 0.00 0.00 4.8 -9937 112095 103 10040
8 25 81 12 0 True 2024-10-27 16:30:00+00:00 2 2 9 False ... 0 0.00 0.00 0.00 0.00 4.8 -1519 111488 1054 2573
9 25 96 15 1 False 2024-11-02 12:30:00+00:00 1 0 10 False ... 0 0.00 0.01 0.01 0.08 4.8 -1515 110916 1859 3374
10 25 103 6 0 False 2024-11-10 16:30:00+00:00 1 1 11 False ... 0 0.00 0.00 0.00 0.00 4.8 -1711 109869 1132 2843
11 25 111 16 1 True 2024-11-23 15:00:00+00:00 3 0 12 False ... 0 0.00 0.00 0.00 0.17 4.8 -928 109888 1706 2634
12 25 129 19 1 False 2024-11-30 17:30:00+00:00 2 5 13 False ... 0 0.00 0.02 0.02 0.74 4.8 -807 109626 1086 1893
13 25 131 14 5 True 2024-12-04 20:15:00+00:00 2 0 14 False ... 1 0.06 0.01 0.07 0.14 4.8 -1118 108914 960 2078
14 25 145 9 0 False 2024-12-08 14:00:00+00:00 1 1 15 False ... 0 0.00 0.00 0.00 0.00 4.8 2940 112774 4379 1439

15 rows × 37 columns

Fetch the current top 50 ranked FPL teams#

[9]:
pb.fpl.get_rankings(1)
[9]:
id event_total player_name rank last_rank rank_sort total entry entry_name has_played
0 56463977 61 yassine hilmi 1 1 1 1114 6790112 3ALLA ALLAH True
1 50941237 69 Ayoub NACIR 🇲🇦 2 5 2 1101 6251129 BB GW1 True
2 4069405 73 Niels Pretorius 3 13 3 1097 649015 Holy Boehly True
3 20948541 75 Dani Lio messi 🇪🇹 3 16 4 1097 3094856 LIV True
4 63597878 67 Ollie Stannard 5 7 5 1095 7550131 hazardous behaviour True
5 58297199 79 abdallah ali ali 6 35 6 1094 6987961 abdo abdo True
6 15255905 76 Johnny Rouge 7 33 7 1091 2315419 Obi Wan - Kenobi Nil True
7 33809310 78 abdullah alanazi 8 44 8 1089 4684786 ugly squad True
8 33179096 67 Doug Yellowley 9 19 9 1088 4605389 The Notorious D.U.G True
9 60289692 66 Abdullah AlGhfeli 9 17 10 1088 7203284 Jürgen Klopp True
10 17356161 66 Sion Nierada 9 15 11 1088 2607470 Snoop Udogie Dogg True
11 4227781 54 Mustafa YNWA 12 4 12 1087 673745 مصطفى فؤاد True
12 13655665 54 Pipat Boss 12 3 13 1087 2085641 Pipat FC True
13 40588476 60 Dara Bbat 12 8 14 1087 5478789 BurnDemAll True
14 5472397 58 abdul hendricks 15 6 15 1086 865299 Pep a pig True
15 1997903 59 Auwal SG 16 11 16 1086 322707 ASG True
16 6385437 50 Harry Jones 17 2 17 1086 1001923 Kobs on the Job True
17 10732157 75 Kostadin Chinov 18 50 18 1085 1661563 Debel lebeD True
18 102501 92 Erin Robinson 19 264 19 1084 18949 Erin’s XI True
19 33875587 89 Elijah Lamb 20 199 20 1084 4693003 Neville Wears Prada True
20 5029234 57 Brendan Sylvester 21 9 21 1084 797171 Bukayo's Sadza True
21 63480780 82 Medo Styen 22 105 22 1083 7537842 الاسطي True
22 1049473 58 Harrison Lenihan 23 12 23 1083 172621 Max O’Deary True
23 72432638 60 Alex Hirkjolen 24 9 24 1083 8448461 Blueberries True
24 6949071 61 vjeran horvat 25 21 25 1081 1091388 Studentgrađanin True
25 48990325 67 Derek SHANKS 26 39 26 1080 6085606 Que Salah Salah True
26 22682175 64 Thanan Nuannukun 27 31 27 1079 3328748 ยอดทีมUnited True
27 12385549 76 JAMIE WALKER 28 85 28 1079 1901957 KobbieManU True
28 5079758 77 Ignaz Vrobel 29 104 29 1079 805343 OlympiqueCliftonHill True
29 41548504 76 Innocent Kalukuta 29 89 30 1079 5554108 Kaluno City True
30 12660145 74 Radhi Alqaisi 31 79 31 1078 1941895 The Seniors True
31 5679387 59 Kristijan Anicic 31 22 32 1078 897442 Stella Rosa True
32 6597581 55 Adrian Wagstyl 33 14 33 1078 1035135 AW True
33 3940951 67 Fahad Al kharafi🇰🇼🇰🇼🇰🇼🇰🇼🇰🇼 34 47 34 1078 628608 I AM PANDA True
34 47259967 69 Mahmoud Ibrahim 35 63 35 1077 5975003 7OoOda True
35 1270775 65 marwen fathallah 36 42 36 1077 206983 back of the Neto True
36 1316826 93 You guy My guy. 37 606 37 1076 214203 Gus@lito. True
37 70637 57 Charlie Shepherd 38 24 38 1075 13609 Charlie’s team True
38 67468283 55 Andrew Warriner 38 20 39 1075 7948422 NYC Whites True
39 114933 61 Mohamed elkabiri 38 37 40 1075 20899 Elkabiri mohamed True
40 15670809 64 the Big studio Ug 41 45 41 1075 2370962 Big Football True
41 13400633 69 hossam Mohamed 41 74 42 1075 2048395 H&M True
42 15704915 74 Remon Radany 41 109 43 1075 2375847 Remon FC True
43 4564586 88 Yaser Hameed 41 440 44 1075 726395 The Leader Yaser True
44 2363269 53 Tom Warlow 45 17 45 1075 378441 TAAnnihilation True
45 73241993 59 Agustio Faizy 46 30 46 1075 8531841 Muthe #1 SSK True
46 2926297 65 James White 47 53 47 1075 468901 Joelinton Enjoyer True
47 39761288 62 Andrew Monaghan 48 40 48 1074 5400771 The Vampires True
48 34694562 85 Sarah Henshaw 49 351 49 1074 4793916 Fly me to the Muniz True
49 3983208 59 Oluchukwu Obiyo 49 31 50 1074 634991 Vee Fc True

Fetch the FPL teams ranked 51-100#

[10]:
pb.fpl.get_rankings(2)
[10]:
id event_total player_name rank last_rank rank_sort total entry entry_name has_played
0 32633580 55 Max Littleproud 51 23 51 1074 4538028 Levenshulme Utd True
1 20253729 83 Harrison Smith 52 314 52 1073 3002723 chicken tika masala True
2 8565725 66 Bu Hussain 53 68 53 1073 1338837 Bu Hussain (BH) True
3 20749224 64 Abd Alwahhab Mohammad 54 60 54 1072 3068916 ربك كريم True
4 35683261 71 Come On You Surds 55 113 55 1072 4906982 Van de Ven Diagrams True
5 24058206 71 Chris Tabor 55 113 56 1072 3506521 Marginal Baines True
6 48394075 69 Einar Birkir Einarsson 55 83 57 1072 6045876 Saga Club True
7 6193323 70 bakri ahmed 58 99 58 1072 972797 najm True
8 6825449 61 Peter Amante-Roberts 59 48 59 1071 1071595 ZlatansZanyZerglings True
9 2830836 82 Reidar Buskenes 60 359 60 1071 453429 Veiholmen True
10 34212118 55 Ray Andre 61 28 61 1071 4735236 Turkish De Ligt True
11 1453653 74 Mohamed Hosny 61 163 62 1071 235899 الكابتن مدحت فقوسة True
12 4954088 69 Patrick O'Loghlen 63 106 63 1070 785876 Xhakaboom True
13 3995317 61 Simon Baker 64 57 64 1070 636988 Marescapetovictory True
14 29477281 73 james delaney 64 163 65 1070 4134522 Holy Boehly True
15 34073001 90 Edward Campbell 66 801 66 1070 4717706 Saka Potatoes True
16 10448238 52 Jack Brennan 67 27 67 1070 1620838 Hakuna Mateta True
17 913873 61 Stephen Blakemore 67 58 68 1070 150191 Womp, womp. True
18 7427609 55 Karim Radi 69 36 69 1070 1166488 Flying Dutchman ��☠ True
19 8893532 73 Joe O’Callaghan 70 176 70 1069 1389327 Mo Problem True
20 31821094 66 Matt Stephenson 71 85 71 1069 4435464 B’croft Big Bears FC True
21 7163746 57 Eric Scott 72 40 72 1069 1125100 The Difference True
22 1614372 53 youssif mohamed 73 29 73 1069 261538 VENOOM True
23 1184864 74 Matt Biemans 73 211 74 1069 194081 Team Biemans True
24 726502 84 Allan Omanovic 73 507 75 1069 120291 Bullyboy True
25 10489134 61 Harry Nielsen 76 65 76 1068 1627092 Harryberry True
26 2486451 66 Jake Poplawski 77 94 77 1068 398230 Feel Wood Inc True
27 6219817 67 Chris Moss 77 113 78 1068 976142 Eze Does It True
28 6682894 64 David Marco 77 79 79 1068 1048988 freepogba True
29 24324008 50 Paras Poudel 80 26 80 1068 3541677 Ne Funk True
30 34270552 70 Anna Bowman 80 151 81 1068 4742695 Yoro wizard Harry True
31 3633448 91 David Cooney 82 988 82 1068 580349 The40YearOldVirgil True
32 1794467 107 Dale Williams 82 3213 83 1068 290231 Harlows Dreamers True
33 36011005 85 Haksbk 3lafekra 84 643 84 1067 4946679 True
34 9120547 72 Alex Lindholm 85 196 85 1067 1424600 Alex True
35 5717441 75 المهلهل …. 86 266 86 1067 903279 الكشاف True
36 1886999 71 Ciarán Rowland 86 180 87 1067 305054 Rainy Night in Szobo True
37 3728651 58 Robb Lacey 86 55 88 1067 595633 Trust the Process ;) True
38 29920 84 Mikołaj Ćwirko 89 618 89 1067 5924 Świry True
39 5242282 74 salim Arbi 90 260 90 1067 830613 Salim Madrid True
40 4603571 60 Max Matthews 91 73 91 1066 732660 Mount Kilman-jaro True
41 60650153 65 Mohammed Hassan 92 107 92 1066 7241449 محمد حسن True
42 61131071 64 souhaib Algérie 93 90 93 1066 7291676 Khenchela team � True
43 2545591 71 Emil Appleson 93 199 94 1066 407494 Endo’s Men True
44 5023130 75 klimis xanthopoulos 95 286 95 1066 796202 Makis True
45 213151 74 Mohammed Haji 95 264 96 1066 36655 ســـرێـــشــمــەیــی True
46 62302397 73 David Ionescu 97 247 97 1066 7415805 SlotMeOut True
47 5086911 81 Rafael Mathisen 97 494 98 1066 806467 FK Matfisen True
48 7864092 68 Craig Davis 97 151 99 1066 1233020 Memyselfandi True
49 2472461 55 Thomas Lawlor 100 46 100 1066 395942 T90 LWR True

Fetch a team’s player picks by gameweek#

[11]:
team_id = 4975559
pb.fpl.get_entry_picks_by_gameweek(team_id, 1)
[11]:
team_id active_chip event points total_points rank rank_sort overall_rank value bank ... player_pick_7 player_pick_8 player_pick_9 player_pick_10 player_pick_11 player_pick_12 player_pick_13 player_pick_14 captain_id is_vice_captain
0 4975559 None 1 55 55 4895408 5033649 4895408 100.0 7.5 ... 16 401 58 82 47 520 44 216 401 372

1 rows × 34 columns

Get entry’s transfer history for this season#

[12]:
pb.fpl.get_entry_transfers(team_id)
[12]:
player_id_in player_in_cost player_id_out player_out_cost entry event time
0 82 7.7 180 7.9 4975559 11 2024-11-08 21:58:38.532929+00:00
1 252 5.7 220 6.0 4975559 9 2024-10-24 19:07:47.537012+00:00
2 180 7.9 4 8.2 4975559 9 2024-10-24 19:07:47.536831+00:00
3 78 5.6 514 7.4 4975559 9 2024-10-24 19:07:47.536620+00:00
4 491 6.7 398 7.3 4975559 9 2024-10-24 19:07:47.536384+00:00
5 99 7.6 317 7.4 4975559 9 2024-10-24 19:07:47.536209+00:00
6 182 10.9 19 5.6 4975559 9 2024-10-24 19:07:47.536018+00:00
7 88 4.5 422 4.5 4975559 9 2024-10-24 19:07:47.535831+00:00
8 162 4.6 369 5.0 4975559 9 2024-10-24 19:07:47.535638+00:00
9 573 4.5 44 4.5 4975559 9 2024-10-24 19:07:47.535371+00:00
10 145 4.5 18 6.0 4975559 9 2024-10-24 19:07:47.535179+00:00
11 152 4.5 413 5.0 4975559 9 2024-10-24 19:07:47.534961+00:00
12 235 4.8 47 5.0 4975559 9 2024-10-24 19:07:47.534506+00:00
13 422 4.5 520 4.4 4975559 8 2024-10-18 07:13:38.487428+00:00
14 4 8.2 82 7.5 4975559 7 2024-10-04 18:12:04.665563+00:00
15 82 7.5 401 8.3 4975559 6 2024-09-28 06:41:03.727840+00:00
16 220 6.0 129 5.6 4975559 6 2024-09-28 06:41:03.721162+00:00
17 514 7.5 342 6.6 4975559 5 2024-09-20 17:50:08.943188+00:00
18 327 7.7 136 6.6 4975559 4 2024-09-14 09:54:52.300593+00:00
19 129 5.7 82 7.5 4975559 4 2024-09-13 16:21:03.307871+00:00
20 136 6.6 16 6.4 4975559 4 2024-09-13 16:21:03.299689+00:00
21 19 5.6 216 5.0 4975559 3 2024-08-31 08:32:51.029245+00:00
22 351 15.0 58 9.0 4975559 2 2024-08-24 08:46:07.977426+00:00
23 342 6.6 372 6.5 4975559 2 2024-08-24 08:46:07.973476+00:00

Get optimal fantasy football squad for a budget of £100m#

[13]:
optimiser_results, team = pb.fpl.optimise_team(budget=100)

optimiser_results
[13]:
{'status': 'Optimal', 'total_points': 1170.0, 'total_price': np.float64(99.6)}
[14]:
team
[14]:
id full_name team position price total_points
0 383 André Onana MUN GKP 5.2 65
1 443 Matz Sels NFO GKP 4.7 62
2 339 Virgil van Dijk LIV DEF 6.4 64
3 3 Gabriel dos Santos Magalhães ARS DEF 6.2 60
4 422 Ola Aina NFO DEF 4.9 58
5 238 Ashley Young EVE DEF 4.6 57
6 211 Daniel Muñoz CRY DEF 4.7 50
7 328 Mohamed Salah LIV MID 13.3 151
8 182 Cole Palmer CHE MID 11.1 125
9 99 Bryan Mbeumo BRE MID 7.6 96
10 247 Alex Iwobi FUL MID 5.7 76
11 230 Dwight McNeil EVE MID 5.4 65
12 447 Chris Wood NFO FWD 6.5 88
13 541 Matheus Santos Carneiro Da Cunha WOL FWD 7.1 80
14 110 Yoane Wissa BRE FWD 6.2 73

Get optimal fantasy football first eleven if playing 3-5-2 if spending £85m#

[15]:
optimiser_results, team = pb.fpl.optimise_team(formation="1-3-5-2", budget=85)

optimiser_results
[15]:
{'status': 'Optimal', 'total_points': 966.0, 'total_price': np.float64(84.5)}
[16]:
team
[16]:
id full_name team position price total_points
0 383 André Onana MUN GKP 5.2 65
1 339 Virgil van Dijk LIV DEF 6.4 64
2 3 Gabriel dos Santos Magalhães ARS DEF 6.2 60
3 422 Ola Aina NFO DEF 4.9 58
4 328 Mohamed Salah LIV MID 13.3 151
5 182 Cole Palmer CHE MID 11.1 125
6 17 Bukayo Saka ARS MID 10.5 103
7 99 Bryan Mbeumo BRE MID 7.6 96
8 247 Alex Iwobi FUL MID 5.7 76
9 447 Chris Wood NFO FWD 6.5 88
10 541 Matheus Santos Carneiro Da Cunha WOL FWD 7.1 80
[ ]: