Author Posts

July 9, 2017 at 8:40 am

Hi,
I managed to capture text about holidays like this: Text capture

How can I format/convert it to objects so I can use it like here, where the dates where in a json file?
Json

July 9, 2017 at 11:35 am

Output the values to pscustomobject

July 9, 2017 at 5:18 pm

$test2 = [pscustomobject]$test

This converts it to a object i guess, but it is still one piece of text.
How can i format it to have a name and date collum, with this kind of date format?:
formating

July 9, 2017 at 11:44 pm

$uri = 'http://www.officeholidays.com/countries/albania/index.php'
$html = Invoke-WebRequest -Uri $uri
$table = $html.ParsedHtml.getElementsByTagName('tr') |
Where-Object {$_.classname -eq 'holiday'} |
Select-Object -exp innerHTML

foreach ($t in $table){
    [void]($t -match 'SPAN title="(.*?)"') ; $m1 = $Matches[1]
    [void]($t -match 'tooltip>(.*)') ; $m2 = $Matches[1]
    [void]($t -match 'remarks>(.*) ') ; $m3 = $Matches[1]
    [PSCustomObject]@{
        Date = $m1 ; Holiday = $m2
        Remarks = If ($m2 -ne $m3){$m3}}
}
# Results
Date                       Holiday                    Remarks                  
----                       -------                    -------                  
January 01                 New Year's Day                                      
January 02                 Day after New Years Day                             
March 14                   Summer Day                                          
March 21                   Nevruz                     Spring Festival. Persi

July 10, 2017 at 8:12 pm

Thanks. Looks like where on to something.

Tested with Alabnia as country, and added DateTime to the variable and got the correct format.
Working

Then tested with Norway as country, and got it like this:
Not working

With Norway and several other countries there is also a class named publicholiday.

Would I have to alter this script for each contry that's difrent?

September 13, 2017 at 3:42 am

Check this tool: json formatter
Hope this help!

September 19, 2017 at 1:58 pm

Hello Alexander,
Please use the below code and let me know if it works:


# Read data from URI
$uri = 'http://www.officeholidays.com/countries/albania/index.php'
$html = Invoke-WebRequest -Uri $uri

# Get the column header and save it into a list - $header_list
$header_table = $html.ParsedHtml.getElementsByTagName('TR')
$data = $header_table[0].cells
$header_list = New-Object System.Collections.ArrayList

foreach($i in $data) {
    $i.outerText
    $header_list.add($i.outerText)
}

# Get the holiday data
$data_table = $html.ParsedHtml.getElementsByTagName('TR') | where {$_.classname -eq 'holiday'}
$final_object_list = New-Object System.Collections.ArrayList

foreach($i in $data_table) {
$header_count = 0
    $single_row_entry = $i.cells
    $object = New-Object System.Object
    foreach ($j in $single_row_entry){
        $value = $j.outertext

        if ($j.outerText -eq $null){
            $value = " "
        }
        $object | Add-Member –MemberType NoteProperty –Name $header_list[$header_count++] –Value $value
    }
    # Print each object that we are creating
    write-host ($object | format-list | out-string)
    $final_object_list.Add($object)
    Write-Host " "
}

# Convert the object to JSON
$json_Object = $final_object_list | ConvertTo-Json