PoshCode Archive  Artifact [cae88fdcaa]

Artifact cae88fdcaa4761f1d8b441cb85d3e7fd9fe737a4a02fd6213406ae0a62adb92a:

  • File HTML-Parse-Demo.ps1 — part of check-in [c141a9dc00] at 2018-06-10 14:10:35 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: 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 }
}