# 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: 6213
# x-archived: 2016-03-19T01:57:21
# x-published: 2016-02-11T15:53: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 }
}