Solved

Powershell Split String

Posted on 2013-06-09
4
761 Views
Last Modified: 2013-06-09
This code was working last year I think something changed with the Data string parse portion and its causing the whole thing to fail.. any sugustions

# ==============================================================================================
# Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2011
# NAME: GetNFLSchedule.ps1
# DATE  : 11/13/2011
# ==============================================================================================

## - Download webpage to a PSObject variable
$source = "http://espn.go.com/nfl/schedule";
$wc = New-Object System.Net.WebClient;
[string] $content = $wc.DownloadString($Source);
[Array] $arrtxt = $content.Split("`n`r");
$x = 0; $arrtxt2 = $null; $rec = 0;





[Array] $NFLScheduleTable = foreach($i in $arrtxt)
{
	## - Get Game Week:
	if (($i -like '*<table class="tablehead"*') -and ($i -like "*</a>Week*")) 
	{
		#Write-Host "Found Week" -ForegroundColor Yellow;
		$getWeekNumber = ""; $WeekNumber = "";
		$s = $i.Substring($i.IndexOf('</a>Week'));
		$s2 = $s.Substring($s.IndexOf('>')+1);
		$y = $s2.Substring($s2.IndexOf('<span><a href'));
		$getWeekNumber = ($s2.Substring(0,($s2.length)-($y.length))).Split(" ");
		$WeekNumber = $getWeekNumber[1].ToString();	
		$getDateText = $true;
	}
	
	## - Get Game Date:
	if (($getDateText -eq $true) -and ($i -like '*<td width="240">*'))
	{
		$DateText = ""; 
		$s = $i.Substring($i.IndexOf('<td width="240">'));
		$s2 = $s.Substring($s.IndexOf('>')+1);
		$y = $s2.Substring($s2.IndexOf('</'));
		$DateText = ($s2.Substring(0,($s2.length)-($y.length)));
		$getDateText = $false;
	}	
	
	## - Get Teams:
	 if($i.ToString() -Match "<td><a href=""http://espn.go.com/nfl/team/_/name/")
	 {
		 $found = $true;
		 $s = $i.substring($i.Indexof("/name/"),$i.IndexOf("</a>")-$i.Indexof("/name/"));
		 $TeamA = $s.Substring($s.indexof(">")+1);
		  
		 $s = $i.substring($i.Indexof("</a> at <a"),$i.IndexOf("</a></td>")-$i.Indexof("</a> at <a"));
		 $AtTeamB = $s.Substring($s.indexof(""">")+2)
		
		## - Get Game Time:
		if($arrtxt[$rec+1].ToString() -Match " PM</td>") 
		 {
				$s = $arrtxt[$rec+1].Substring($arrtxt[$rec+1].IndexOf('<td>'));
				$s2 = $s.Substring($s.IndexOf('>')+1);
				$y = $s2.Substring($s2.IndexOf('</'));
				$TimeText = ($s2.Substring(0,($s2.length)-($y.length)));
		 }
	 };

	## - Build the Game Schedule PSobject:
	if($found)
	{ 
		$found = $false; $x++;		
		$GetNFLScheduleLine = @{
			seq				= ([int32] $x);
			Week			= ([int32] $WeekNumber);
			GameDate		= $DateText.ToString();
			GameTime		= $TimeText;
		    TeamA			= $TeamA.ToString();
		    AtTeamB			= $AtTeamB.ToString();	    
		}
		
		$NFLScheduleRecord = New-Object PSObject -Property $GetNFLScheduleLine;
		$NFLScheduleRecord;
	};
    

    
	$rec++;
}

## - Display on screen:
$NFLScheduleTable | sort seq | select seq, Week, GameDate, GameTime, TeamA, AtTeamB | ft -AutoSize

Open in new window

0
Comment
Question by:Leo Torres
  • 2
  • 2
4 Comments
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39233340
Bad way to parse a Web page ... It depends on table column widths, which is not reliable. Any change of the page will result in errors.
In this case, replace this part (lines 33 to 42):
	## - Get Game Date:
	if (($getDateText -eq $true) -and ($i -like '*<td width="170">*'))
	{
		$DateText = ""; 
		$s = $i.Substring($i.IndexOf('<td width="170">'));
		$s2 = $s.Substring($s.IndexOf('>')+1);
		$y = $s2.Substring($s2.IndexOf('</'));
		$DateText = ($s2.Substring(0,($s2.length)-($y.length)));
		$getDateText = $false;
	}	

Open in new window

0
 
LVL 8

Author Comment

by:Leo Torres
ID: 39233387
Great !! worked..
Any suggestion if this is not best method.. How might have you done different
0
 
LVL 8

Author Closing Comment

by:Leo Torres
ID: 39233388
Thanks!
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39233453
Without analyzing the site in depth, I would parse it as XML, or use GetElementByTagName and the like. This allows for selecting the third column in the second table and such things.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to dynamically set the form action using jQuery.

758 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

22 Experts available now in Live!

Get 1:1 Help Now