I imported and cleaned the following datasets for england soccer league in order to have home/away rank tables and next round matches:

Cl = Import["https://www.soccerstats.com/homeaway.asp?league=england",
Chome = Drop[Drop[Cl[[2, 4, 1]]], 1];
Caway = Drop[Drop[Cl[[2, 4, 2]]], 1];

dataENG = 
pmtype=bydate", "Data"];
Drop[Drop[Drop[Cases[dataENG, {_, _, _, _}, Infinity], -4], -1, None],
   None, -1];
   If[StringContainsQ[%[[i, 2]], ":"] == True, %[[i]], ## &[]], {i, 1,
     Length[%]}], Length[teamsENG]/2];
Table[StringSplit[%[[i]], "-"], {i, 1, Length[%]}];
matchesENG = 
  Transpose[{StringTrim[%[[All, 3, 1]]], StringTrim[%[[All, 3, 2]]]}];

Instead of repeating the code for each european league, taking into account that the "root" of the webpages is the same for all leagues, that is:


where ...=nation's name, I would like to implement a function to map the nations with the webpages in order to have within the Chome, Caway and matches vectors the data of all main european leagues.

  • $\begingroup$ What is teamsENG? $\endgroup$ Commented Feb 11, 2020 at 1:42
  • $\begingroup$ Sorry, teamsENG=Chome[[All,2]] $\endgroup$
    – Nate
    Commented Feb 11, 2020 at 10:58

3 Answers 3



To get a list containing the Home values for all of the countries as separate sub lists:

allHome = countryMatches // Map[KeyTake[#, "Home"] &] // Catenate

Similarly for any other key in the association.

I modified the code so it is easier (at least for me) to understand and wrapped it in a function that returns an Association of associations that hold match and team data.

matchesFor[country_] := Module[
  {data, home, away, teams, allGames, byDate, matches1, matches2, allMatches,
   selectedMatches, matchTeams},

  data = Import["https://www.soccerstats.com/homeaway.asp?league=" <> country, "Data"];
  home = data[[2, 4, 1, 2 ;;]];
  away = data[[2, 4, 2, 2 ;;]];
  teams = home[[All, 2]];

  byDate = Import["https://www.soccerstats.com/results.asp?pmtype=bydate&league=" <> country, "Data"];
  matches1 = byDate[[2, 3, 2, 1, All, 1 ;; -2]] // Select[Length@# == 3 &];
  matches2 = byDate[[2, 3, 2, 2, 1, All, 1 ;; -2]];
  allMatches = matches1~Join~matches2;

  selectedMatches = allMatches // Select[StringContainsQ[#[[2]], ":"] &] // 
     Take[#, Length[teams]/2] & // Map[StringSplit[#, " - "] &];

  matchTeams = selectedMatches[[All, 3]];

  <|country -> <|"Teams" -> teams, "Home" -> home, "Away" -> away, 
     "Matches" -> allMatches, "SelectedMatches" -> selectedMatches, 
     "MatchTeams" -> matchTeams|>|>

Then Map the function over the list of countries you are interested in and combine the associations.

countryMatches = matchesFor /@ {"England", "France", "Germany"} // Association;

To get the data for a country


Thank you Rohit! Actually I would need to have all the main european leagues data within three big vectors only, that is Chome, Caway and matches. Basically, I would like to have Chome={ChomeENG,ChomeITA,ChomeGER} by manipulating website pages root with a function which creates it. And so on for Caway and matches, instead of doing it by repeating the same code form to import data from all leagues to create n subvectors like ChomeENG, ChomeITA, ChomeGER(etc.) with several code strings equal to each others unless for country name.

  • 1
    $\begingroup$ Mick, I am not sure exactly what you mean. Since the countryMatches association has the data for all countries, it is easy to combine data from different countries. I have updated the answer with an example. $\endgroup$ Commented Feb 12, 2020 at 18:45
  • $\begingroup$ Rohit, awesome! It works! Thank you!!! $\endgroup$
    – Nate
    Commented Feb 13, 2020 at 18:19
  • $\begingroup$ Hi Rohit, I posted a similar issue here: mathematica.stackexchange.com/questions/216991/… can you help me? $\endgroup$
    – Nate
    Commented Mar 26, 2020 at 15:13

In order to test the model, I should implement within the matchesFor function the importation of the already played matches dataset ordered by rounds for all leagues (here reported Italian league code from the webpage that already does this filter):

d = Import["https://www.soccerstats.com/results.asp?league=italy&pmtype=\bygameweek","Data"];
t2=Table[{d[[2,3,2,1,2,i,1,1,1]],Drop[Drop[Drop[d[[2,3,2,1,2,i,1]],1],None,1],None,-1]},{i, 1, 4}];
Join[t1, t2];

I guess it should be filtered also by allGames variable with some If condition?

  • $\begingroup$ Not sure what exactly you mean by "filtered". BTW your code is missing Round 24. Always better to use Map rather than Table with hardcoded limits. Also, please do not use answers to ask new questions, Ask a new question and link to the original. The moderators are likely to delete this and your previous answer as "Not an answer". $\endgroup$ Commented Feb 15, 2020 at 22:07
  • $\begingroup$ ok, sorry. I'll post a new question, thank you! $\endgroup$
    – Nate
    Commented Feb 16, 2020 at 7:40
  • $\begingroup$ I hope it is understandable what I would like to do, I posted a new question here: mathematica.stackexchange.com/questions/215230/… $\endgroup$
    – Nate
    Commented Feb 24, 2020 at 12:08
  • 1
    $\begingroup$ Hi Nate I don’t have time now. Will take a look over the weekend. $\endgroup$ Commented Jul 25, 2020 at 2:23
  • 1
    $\begingroup$ The structure has changed. Try allMatches = byDate[[2, 4, 1]]. Also, would be nice if you could upvote my first answer if it helped you. Thanks! $\endgroup$ Commented Jul 26, 2020 at 23:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.