# encoding: ascii
# api: powershell
# title: HTML Parse Demo
# description: This is a very meaningless demo showing how to get and work with a html document in parsed form. The actual script gets a html-table from www.apk.se (listing cheap alcohol in sweden) and parses and converts that to a object and hands that of to the pipe.
# version: 0.1
# author: Daniel Srlv
# license: CC0
# x-poshcode-id: 3664
# x-archived: 2017-03-10T20:33:31
# x-published: 2013-09-26T07:11:00
#
#
$page = Invoke-WebRequest "http://www.apk.se"
$html = $page.parsedHTML
$products = $html.body.getElementsByTagName("TR")
$headers = @()
foreach($product in $products)
{
	$colID = 0;
	$hRow = $false
	$returnObject = New-Object Object
	foreach($child in $product.children)
	{	
		if ($child.tagName -eq "TH")
		{
			$headers += @($child.outerText)
			$hRow = $true
		}

		if ($child.tagName -eq "TD")
		{
			$returnObject | Add-Member NoteProperty $headers[$colID] $child.outerText
		}
		$colID++

	}
	if (-not $hRow) { $returnObject }
}
