Author Posts

March 20, 2016 at 1:01 pm

Hi Folks,

I'm working on something fun with my son who is a soccer fanatic. I'm taking down the squad details from http://uefa.com/uefaeuro for the upcoming European Championships.

$squads = Invoke-WebRequest -Uri "http://www.uefa.com/uefaeuro/season=2016/teams/team=64/squad/index.html"
($squads.ParsedHtml.getElementsByTagName('td')| Where{$_.InnerText}).Innertext

The squad data for each team comes out in a long string of player attributes. It's not totally unstructured. Although there are no headings, values for Name, Birthdate, Age, Club, Caps, Goals are represented in the data for each player.

Example below.

David McGoldrick
29/11/1987
28
Ipswich


Daryl Murphy
15/03/1983
33
Ipswich
5

Anthony Stokes
25/07/1988
27
Hibernian


Jon Walters
20/09/1983
32
Stoke
10
5

I'm looking to get the returned data into an object for each player.

ConvertFrom-String and PSCustomObject looked promising but I couldn't pull it off. Is there a PowerShell method to handle this or should I consider a While loop?

Thanks,

Michael

March 20, 2016 at 4:46 pm

# Name, Birthdate, Age, Club, Caps, Goals are represented in the data for each player.
$squads = Invoke-WebRequest -Uri "http://www.uefa.com/uefaeuro/season=2016/teams/team=64/squad/index.html"
$data = ($squads.ParsedHtml.getElementsByTagName('td')| Where{$_.InnerText}).Innertext

$results = for ($i = 0; $i -lt $data.count; $i += 6)
{
    [PSCustomObject]@{
        Name = $data[$i]
        Birthdate = $data[$i+1]
        Age = $data[$i+2]
        Club = $data[$i+3]
        Caps = $data[$i+4]
        Goals = $data[$i+5]
    }
}

# Sample outputs - pick one or more, your choice
$results
$results | Format-Table -AutoSize
$results | Out-GridView
$results | Export-Csv -Path .\foo.csv -NoTypeInformation -Encoding ASCII
$results | Out-File -FilePath .\foo.txt -Encoding ASCII
$results | Export-Clixml -Path .\foo.xml -Encoding ASCII

March 20, 2016 at 11:30 pm

That's perfect Bob thanks

March 21, 2016 at 1:26 am

Yay for structured data! It made getting to solution pretty easy.