Powershell Get Table From Web Into CSV Instructions



Powershell Get Table From Web Into CSV Instructions1. Save following as file GetTableFromWebIntoCSV.ps1# GetTableFromWebIntoCSV.ps1 - Read a table from Web# Source Article: Calls Get-WebRequestTable.ps1$url = ''$r = Invoke-WebRequest $url.\Get-WebRequestTable.ps1 $r -TableNumber 0 | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | Set-Content -Path .\test.csv 2. Save following as file Get-WebRequestTable.ps1# Read a table from Web# Source Article: Use GetTableFromWebIntoCSV.ps1 to runparam( [Parameter(Mandatory = $true)] [Microsoft.mands.HtmlWebResponseObject] $WebRequest, [Parameter(Mandatory = $true)] [int] $TableNumber)## Extract the tables out of the web request$tables = @($WebRequest.ParsedHtml.getElementsByTagName("TABLE"))$table = $tables[$TableNumber]$titles = @()$rows = @($table.Rows)## Go through all of the rows in the table$i=0foreach($row in $rows){ $cells = @($row.Cells) ## If we've found a table header, remember its titles if($cells[0].tagName -eq "TH") { $titles = @($cells | % { ("" + $_.InnerText).Trim() }) continue } ## If we haven't found any table headers, make up names "P1", "P2", etc. if(-not $titles) { $titles = @(1..($cells.Count + 2) | % { "P$_" }) } ## Now go through the cells in the the row. For each, try to find the ## title that represents that column and create a hashtable mapping those ## titles to content $resultObject = [Ordered] @{} for($counter = 0; $counter -lt $cells.Count; $counter++) { $title = $titles[$counter] if(-not $title) { continue } $resultObject[$title] = ("" + $cells[$counter].InnerText).Trim() } ## And finally cast that hashtable to a PSCustomObject [PSCustomObject] $resultObject} 3. Invoke using following command line prompt> powershell -file GetTableFromWebIntoCSV.ps1Output file is test.csv ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download