Solved

powershell parse

Posted on 2014-11-26
5
118 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 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
Cool works!!!

Can you elaborate on this logic here please

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

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
last logon 2 12
Get String split 5 30
Modifying Powershell script to change service startup mode ? 9 17
Achieve json result 2 20
Utilizing an array to gracefully append to a list of EmailAddresses
A procedure for exporting installed hotfix details of remote computers using powershell
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now