Solved

Powershell Split String

Posted on 2013-06-09
4
764 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 69

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 69

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
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ā€¦
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

770 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