Stream of data convert to object

This topic contains 4 replies, has 3 voices, and was last updated by  Bob McCoy 1 year, 8 months ago.

  • Author
    Posts
  • #36778

    Michael Maher
    Participant

    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

  • #36779

    Arie H
    Participant
  • #36782

    Bob McCoy
    Participant
    # 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
  • #36787

    Michael Maher
    Participant

    That's perfect Bob thanks

  • #36792

    Bob McCoy
    Participant

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

You must be logged in to reply to this topic.