0

I try to scrape an HTML page using PS. I want to retrieve a specific DIV class attrs Here is my code.

    $url = 'https://www.besoccer.com/competition/1_division_belarus'
$req =  Invoke-WebRequest -Uri $url -Method Get

if ($req.StatusCode -ne 200) {
    Write-Warning "Error: Website returned statuscode $($req.StatusCode)"
}
else {
          $date = ($req.ParsedHtml.getElementsByTagName('div') | where {$_.classname -eq 'date-transform date ta-c'}).innerHTML
          $date += ($req.ParsedHtml.getElementsByTagName('div') | where {$_.classname -eq 'match-status-label'}).InnerText
          $homet = ($req.ParsedHtml.getElementsByTagName('div') | where {$_.classname -match 'team-name ta-r team_left'}).InnerText
          $homea = ($req.ParsedHtml.getElementsByTagName('div') | where {$_.classname -match 'team-name ta-l team_right'}).InnerText
          $res = [pscustomobject]@{
    Date = $($date)
    Time = $($time)
    Home = $($homet)
    Away = $($homea)
}
#$res
}
$res

But I receive the following result

Date                                                    Time Home                                                    Away                                                  
----                                                    ---- ----                                                    ----                                                  
{20 MAY 2022, 21 MAY 2022, 22 May 2022, 22 May 2022...}      {Baranovichi, FK Smorgon, Dnepr Rogachev, FC Slonim...} {Volna Pinsk, Osipovichy, Naftan Novopolotsk, Lokom...

How can I receive data as table (format-table)?

1 Answer 1

1

You need to parse DIV class inside each event e.g. as follows:

$url = 'https://www.besoccer.com/competition/1_division_belarus'
$req =  Invoke-WebRequest -Uri $url -Method Get

if ($req.StatusCode -ne 200) {
    Write-Warning "Error: Website returned statuscode $($req.StatusCode)"
} else {
    $Events = $req.ParsedHtml.getElementsByTagName('a') |
        Where-Object {$_.id -match "^match-"}
    $res = foreach ($Event in $Events) {
        $date  = ($Event.getElementsByTagName('div') | where {$_.classname -eq 'date-transform date ta-c'}).innerHTML
        $time  = ($Event.getElementsByTagName('div') | where {$_.classname -eq 'match-status-label'}).InnerText
        $homet = ($Event.getElementsByTagName('div') | where {$_.classname -match 'team-name ta-r team_left'}).InnerText
        $homea = ($Event.getElementsByTagName('div') | where {$_.classname -match 'team-name ta-l team_right'}).InnerText
        [pscustomobject]@{
            Date = $date
            Time = $time
            Home = $homet
            Away = $homea
        }
    }
    $res
}

Output: .\SO\72332736.ps1

Date        Time Home           Away              
----        ---- ----           ----              
20 MAY 2022 End  Baranovichi    Volna Pinsk       
21 MAY 2022 End  FK Smorgon     Osipovichy        
22 MAY 2022 End  Dnepr Rogachev Naftan Novopolotsk
22 MAY 2022 End  FC Slonim      Lokomotiv Gomel   
22 MAY 2022 End  Molodechno     Shakhtyor Petrikov
22 MAY 2022 End  Ostrovets      FK Lida           
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.