Solved

powershell parse

Posted on 2014-11-26
5
121 Views
Last Modified: 2014-11-26
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

0
Comment
Question by:Leo Torres
  • 3
  • 2
5 Comments
 
LVL 69

Expert Comment

by:Qlemo
ID: 40467998
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

0
 
LVL 8

Author Comment

by:Leo Torres
ID: 40468151
Cool works!!!

Can you elaborate on this logic here please

IF ("stathead", "colhead" -notcontains $col.Attributes["class"].value)
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40468182
The "colhead" was already excluded, I added "stathead" as that is the datetime header.
("a", "b" -notcontains "a") is $true if "a" is not an element of the array "a", "b" - it is here, so it will result in $false. The same happens if we meet the date and time header, and we are not displaying that. (As we only want to have a single day, the header is of no use.)

The remaining changes are just to make sure we only process the very first table = one day. No loop to go thru the days retrieved.
0
 
LVL 8

Author Comment

by:Leo Torres
ID: 40468190
Great. I will try to insert this into a database.

I will give it a stab. I will post if I get stuck thanks
0
 
LVL 8

Author Closing Comment

by:Leo Torres
ID: 40468191
Thanks
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

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