Link to home
Start Free TrialLog in
Avatar of Leo Torres
Leo TorresFlag for United States of America

asked on

Powershell select leaf nodes

I believe I in the right top node. I how do I traverse down to the node I need. Code is attached and a Screen shot of Node I am on and node I want to get to. I want to return the "UTA" for away team.

Add-Type -path C:\pstemp\HtmlAgilityPack\Net40\htmlagilitypack.dll

CLS

$Website = "http://www.nba.com/gameline/20151031/"
$wc = New-Object System.Net.WebClient;
$doc = New-Object HtmlAgilityPack.HtmlDocument
$doc.LoadHtml($wc.DownloadString($Website))

$games = @()
$Allgames = @()

foreach ($node1 in $doc.DocumentNode.SelectNodes("//div[@class='nbaModOuterBox gameModule']"))
{

	foreach ($node2 in $node1.DocumentNode.SelectNodes("//div[@class='nbaModTopTeamAw']") )
	{
		$AwayTeam = $node2.Innertext
		Write-Host $AwayTeam
	}
	
}

Open in new window


User generated image
Avatar of Leo Torres
Leo Torres
Flag of United States of America image

ASKER

Made some progress this way but I can seem to get the first 4 records to parse correctly only the last 2

CLS

# HAP available at http://htmlagilitypack.codeplex.com/
# (originally http://www.nuget.org/packages/HtmlAgilityPack)
Add-Type -path C:\pstemp\HtmlAgilityPack\Net40\htmlagilitypack.dll

$Website = "http://www.nba.com/gameline/20151031/"
$wc = New-Object System.Net.WebClient;
$doc = New-Object HtmlAgilityPack.HtmlDocument
$doc.LoadHtml($wc.DownloadString($Website))

$games = @()
$Allgames = @()
foreach ($day in $doc.DocumentNode.SelectNodes("//div[@class='nbaModOuterBox gameModule']"))
{
	$rows = $day.SelectNodes('div')
	$lines = $rows[0].InnerText.split('`n')# | where { $rows[0].InnerText[0] -like ' '  }
        
        $gameTime = $lines[0].substring(0, 7)
	$AwayTeam = $lines[1..2].substring(2, 3)
	$HomeTeam = $lines[1..2].substring(5, 3)

	Write-Host $gametime " " $AwayTeam " " $HomeTeam


}

Open in new window

Hi,
could you please explain in words, which parts of the page you want to capture?

As far as I can see, the gametime is only available on the current day, not in the past.

Thanks.
Rainer
Hello Rainer correct. just replace the "/20151031/" at the end of the link to a future date

I need the gametime value and the to teams playing at that time
I'm not clear about the intended output. Can you show a future game result as you expect it to be?
sorry for the delayed response. I need the gametime, and each team. That's  it 3 itmes.
Sample
Gametime                 AwayTeam                Hometeam
     7:30PM                       MIA                             ORL

This is an example this data may not exists I just created off the top of my head. If you want to test the webpage you have to choose a future date in the link. Games in the past do not have a Gametime value.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank You!