Home PowerShell - filter output datas from html table
Reply: 1

PowerShell - filter output datas from html table

Munteanu Doru
1#
Munteanu Doru Published in 2017-09-14 10:28:28Z

I managed to get entire table from a page with the following code, but I want to ask you if you can help me to filter values from 4th column and if value is > 20 to get as output all entire row of specific value (rest will be ignored). Thank you. Is more for learning, but this kind of script is very useful for people that are working with table extraction.

$r = Invoke-WebRequest https://en.wikipedia.org/wiki/Divorce_demography
$data = ( $r.ParsedHtml.getElementsByTagName("table") | Select-Object -First 1 ).rows
$table = @()
forEach($datum in $data){
    if($datum.tagName -eq "tr"){
        $thisRow = @()
        $cells = $datum.children
        forEach($cell in $cells){
            if($cell.tagName -imatch "t[dh]"){
                $thisRow += $cell.innerText
            }
        }
        $table += $thisRow -join ","
    }
}
$table | out-file E:\test.csv -Encoding ascii

this code could be added to script ?

 if ($html.table.tr[$i].td[3] -eq 0) {}
David Brabant
2#
David Brabant Reply to 2017-09-14 12:08:28Z

Not sure I really understand your question, but the last line in your example can probably rewritten something like this:

$table | ConvertFrom-Csv -Header @("Country", "MarriageRate", "DivorceRate", "Ratio", "Year") | ?{ $_.Ratio -gt 20 } | select Country, MarriageRate, DivorceRate, Ratio
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.415411 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO