Powershell, Split String

Code is close but no cigar.

Day and games data in the same column Day should be repeated for each game on that date.

$Week Variable is there a way to just grab data from node? I dont wan to use a replace in case that value changes.

<#
.SYNOPSIS
   <A brief description of the script>
.DESCRIPTION
   <A detailed description of the script>
.PARAMETER <paramName>
   <Description of script parameter>
.EXAMPLE
   <An example of using the script>
#>

CLS
# HAP available at http://htmlagilitypack.codeplex.com/
# (originally http://www.nuget.org/packages/HtmlAgilityPack)
Add-Type -path C:\pstemp\HtmlAgilityPack\Net40\htmlagilitypack.dll
#Add-Type -path C:\pstemp\HtmlAgilityPack\Net20\htmlagilitypack.dll
$Website = 'http://espn.go.com/nfl/schedule/_/year/2012'
$wc  = New-Object System.Net.WebClient;
$doc = New-Object HtmlAgilityPack.HtmlDocument
$doc.LoadHtml($wc.DownloadString($Website))
 
$games = @()
$Allgames = @()
foreach ($day in $doc.DocumentNode.SelectNodes('//table["tablehead"]'))
{
  $rows = $day.SelectNodes('tr')
  $Week = $rows[0].InnerText -replace "back to top &#187;", ""<# [DateTime] #> 
  foreach ($row in $rows[2..$rows.Count])
  {
    $col = $row.Element('td'); 
   $time  = $col.InnerText
  #  $col = $col.NextSibling; 
 	$teams = $col.InnerText[2] #| Select -ExpandProperty InnerText) #-join " at "
    $games += New-Object PsObject -Property @{Week = $Week; Day = $time; Teams = $teams}
  }
}

$Allgames += $games

$Allgames | select Week,Day,@{Name='Games';Expression={[string]$_.Teams}} | Format-Table -AutoSize

Open in new window

LVL 8
Leo TorresSQL DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
Your error is further up in the code
$Allgames += $games
$Allgames | select Week,Day | fl
gives the output of:
Week : Week 17
Day  : Denver 38, Kansas City 3
Week : Week 17
Day  : Washington 28, Dallas 18
0
Leo TorresSQL DeveloperAuthor Commented:
So what is your suggested change.
This line is at the bottom "$Allgames += $games"

The error has to be above this line? What are you suggesting
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Are you aware that that page hasn't the same setup as the one we worked on originally ( http://www.experts-exchange.com/Q_28209977.html )? The concept of "time" (there is none) and "teams" is different here, so you can't apply the same code. $time contains the team results, $teams the third character of $time.

So, back to the basics.
table "tablehead"  is one week (correct).
  tr "colhead"  contains the date
  tr "oddrow" and tr "evenrow" build results
  tr "colhead"  contains the next date
  ...

Since the the dates and games are not set up in a hierarchy, but on the same level, we have to be more clever in detecting. The following is still assuming which Childnodes to take - that could be made more robust by asking for specific types instead ...
CLS
# HAP available at http://htmlagilitypack.codeplex.com/
# (originally http://www.nuget.org/packages/HtmlAgilityPack)
Add-Type -path C:\pstemp\HtmlAgilityPack\Net40\htmlagilitypack.dll
#Add-Type -path C:\pstemp\HtmlAgilityPack\Net20\htmlagilitypack.dll
$Website = 'http://espn.go.com/nfl/schedule/_/year/2012'
$wc  = New-Object System.Net.WebClient;
$doc = New-Object HtmlAgilityPack.HtmlDocument
$doc.LoadHtml($wc.DownloadString($Website))
 
$games = @()
foreach ($day in $doc.DocumentNode.SelectNodes('//table["tablehead"]'))
{
  $rows = $day.SelectNodes('tr')
  $Week = $rows[0].InnerText -replace "back to top &#187;", ""<# [DateTime] #> 
  foreach ($row in $rows | select -skip 1)
  {
    $col = $row # .Element('td'); 
    if ($col.Attributes["class"].value -eq "colhead")
    { # a day
      $day = $col.ChildNodes[1].InnerText
    } else {  # a game
      $teams = $col.ChildNodes[0].InnerText
      $games += New-Object PsObject -Property @{Week = $Week; Day =$day; Games = $teams}
    }
  }
}

$games | select Week,Day,Games | Format-Table -AutoSize

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Leo TorresSQL DeveloperAuthor Commented:
Qlemo,

Thank you this works I need some time to analyze and consume information. I know this one is a bit different but I tried to tackle it based on what I learned from you to see if I could apply new knowledge (I was unsuccessful that's why I put up the question). Some info back just not in the right columns.

Once I review I will open a new question with questions about code so can explain for credit.

Thanks again!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.