[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Powershell Code tune up

Posted on 2014-09-01
5
Medium Priority
?
253 Views
Last Modified: 2014-09-03
This script worked last year
http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28223497.html

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' Site last year
$Website = 'http://espn.go.com/nfl/schedule/_/seasontype/2/week/1' # Site this year
$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


The site has changed and now I cant get the code to work and get Game data.

Code should get Week, Day of Game and Game its self.
0
Comment
Question by:Leo Torres
  • 3
5 Comments
 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 40298569
I cannot say what the HTML looked like when it worked, but now it is a mess.  It is not a well formed XHTML so when it goes to select the nodes with table["tablehead"] it comes up with nothing.  Not much you can do to fix their html.
0
 
LVL 8

Author Comment

by:Leo Torres
ID: 40299358
Yes it was a mess before . What they did this year was now you can select the individual weeks before all the weeks were together.
0
 
LVL 11

Accepted Solution

by:
Mihai Stancescu earned 2000 total points
ID: 40300738
Hi,

For me works ok, I guess.

All I did was to change the reference for html agility from 40 to 20, after downloaded from the link in the script.

Print screen power shell
Regards,
Mishu
0
 
LVL 8

Author Closing Comment

by:Leo Torres
ID: 40301319
That was to easy.. Wow cant believe I couldn't figure that one out.. Thank you for your help!
0
 
LVL 8

Author Comment

by:Leo Torres
ID: 40301322
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:\Users\ltorres2\Downloads\PS4\HtmlAgilityPack.1.4.6\Net20\htmlagilitypack.dll
#$Website = 'http://espn.go.com/nfl/schedule/_/year/2012' Site last year
$Website = 'http://espn.go.com/nfl/schedule/_/seasontype/2/week/1' # Site this year
$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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question