Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

powershell parse

Posted on 2014-11-26
5
Medium Priority
?
131 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 71

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 71

Accepted Solution

by:
Qlemo earned 2000 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

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 recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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…
Loops Section Overview

916 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