PoshCode Archive  Artifact [f73a30e936]

Artifact f73a30e936dff1b7bb1d5b6afa68931893646508886eafbd4f2ad1ca6ee6f790:

  • File HTML-Parse-Demo.ps1 — part of check-in [5878202585] at 2018-06-10 13:46:07 on branch trunk — 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. (user: Daniel Srlv size: 1051)

# 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: 4805
# x-archived: 2015-05-05T01:35:09
# x-published: 2015-01-16T22:22: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 }
}