# Import data from different webpages of the same website

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",
"Data"];
Chome = Drop[Drop[Cl[[2, 4, 1]]], 1];
Caway = Drop[Drop[Cl[[2, 4, 2]]], 1];

dataENG =
Import["https://www.soccerstats.com/results.asp?league=england&\
pmtype=bydate", "Data"];
Drop[Drop[Drop[Cases[dataENG, {_, _, _, _}, Infinity], -4], -1, None],
None, -1];
Take[Table[
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:

"https://www.soccerstats.com/homeaway.asp?league=..."
"https://www.soccerstats.com/results.asp?league=...&\pmtype=bydate"


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.

• What is teamsENG? – Rohit Namjoshi Feb 11 '20 at 1:42
• Sorry, teamsENG=Chome[[All,2]] – Nate Feb 11 '20 at 10:58

Update

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

countryMatches["England"]


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.

• 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. – Rohit Namjoshi Feb 12 '20 at 18:45
• Rohit, awesome! It works! Thank you!!! – Nate Feb 13 '20 at 18:19
• Hi Rohit, I posted a similar issue here: mathematica.stackexchange.com/questions/216991/… can you help me? – Nate Mar 26 '20 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"];
t1=Table[{d[[2,3,2,1,1,i,1,1,1]],Drop[Drop[Drop[d[[2,3,2,1,1,i,1]],1],None,1],None,-1]},{i,1,Length[d[[2,3,2,1,1]]]}];
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?

• 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". – Rohit Namjoshi Feb 15 '20 at 22:07
• ok, sorry. I'll post a new question, thank you! – Nate Feb 16 '20 at 7:40
• I hope it is understandable what I would like to do, I posted a new question here: mathematica.stackexchange.com/questions/215230/… – Nate Feb 24 '20 at 12:08
• Hi Nate I don’t have time now. Will take a look over the weekend. – Rohit Namjoshi Jul 25 '20 at 2:23
• 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! – Rohit Namjoshi Jul 26 '20 at 23:05