Link to home
Start Free TrialLog in
Avatar of Leo Torres
Leo TorresFlag for United States of America

asked on

powershell parse

The code below works to a certain extent. I need to refine it a bit.

2 things I need.

1. I need to only return scores for 20110114

2. I need this make this into a function where I can call it from the command line and pass it a date. In this format

20141126 is an example.

CLS
Add-Type -path C:\pstemp\HtmlAgilityPack\Net40\htmlagilitypack.dll

$Website = "http://espn.go.com/nba/schedule?date=20110114"
$wc = New-Object System.Net.WebClient;
$doc = New-Object HtmlAgilityPack.HtmlDocument
$doc.LoadHtml($wc.DownloadString($Website))

Foreach ($game in $doc.DocumentNode.SelectNodes('.//table["tablehead"]'))
{
	
	$rows = $game.SelectNodes('tr')
	$Day = $rows[0].InnerText
	
	Foreach ($row in $rows)
	{
		$col = $row
		
		IF ($col.Attributes["class"].value -ne "colhead")
		{
			$str2 = $col.ChildNodes[0].InnerText
			Write-host $str2
		}
	}
	
}

Open in new window

Avatar of Qlemo
Qlemo
Flag of Germany image

The function definition is
Add-Type -path C:\pstemp\HtmlAgilityPack\Net40\htmlagilitypack.dll

function get-ESPNScore ([String] $yyyymmdd)
{
  CLS

  $Website = "http://espn.go.com/nba/schedule?date=$yyyymmdd"
  $wc = New-Object System.Net.WebClient;
  $doc = New-Object HtmlAgilityPack.HtmlDocument
  $doc.LoadHtml($wc.DownloadString($Website))

  $game = $doc.DocumentNode.SelectNodes('.//table["tablehead"]') | select -First 1
  	
  $rows = $game.SelectNodes('tr')
  $Day = $rows[0].InnerText

  Foreach ($row in $rows)
  {
  	$col = $row
  	
  	IF ("stathead", "colhead" -notcontains $col.Attributes["class"].value)
  	{
  		$str2 = $col.ChildNodes[0].InnerText
  		Write-host $str2
  	}
  }
}	

Open in new window

After importing that into your PS console by using something along
. C:\Scripts\get-ESPNScore.ps1

Open in new window

you can call it with
get-EPSNScore 20141120

Open in new window

Avatar of Leo Torres

ASKER

Cool works!!!

Can you elaborate on this logic here please

IF ("stathead", "colhead" -notcontains $col.Attributes["class"].value)
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great. I will try to insert this into a database.

I will give it a stab. I will post if I get stuck thanks
Thanks