Solved

powershell parse

Posted on 2014-11-26
5
123 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 70

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 70

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Open telerik datetime picker programmatically 13 42
What is GIS method of Geometry data type? 6 36
Strip 'trailing' spaces 10 36
Active Directory Powershell Script 9 41
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
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 antispam), the admini…

739 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