Skip to content

Instantly share code, notes, and snippets.

@itsthedoc
Created September 3, 2018 16:09
Show Gist options
  • Save itsthedoc/6e704c0500db14310fc6a21a18b6fb6c to your computer and use it in GitHub Desktop.
Save itsthedoc/6e704c0500db14310fc6a21a18b6fb6c to your computer and use it in GitHub Desktop.
$numbers = @(4..6)
foreach ($num in $numbers) {
$WebRequest = Invoke-WebRequest "http://sonlite.dnr.state.la.us/sundown/cart_prod/cart_con_pshmonprod2?p_beg_date=2018&p_parish_code=0$num"
$results = @()
## Extract the tables out of the web request
$tables = @($WebRequest.ParsedHtml.getElementsByTagName("TABLE"))
$table = $tables[0]
$titles = @()
$headers = @($table.rows)[0..1]
$parishcode = (($headers[1]).cells[0]).innertext
$parishname = (($headers[1]).cells[1]).innertext
$year = (($headers[1]).cells[2]).innertext
$rows = @($table.Rows | select -Skip 2)
## Go through all of the rows in the table
foreach($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
}
## 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]
$resultObject[$title] = ("" + $cells[$counter].InnerText).Trim()
$resultObject["Parish Code"] = $parishcode
$resultObject["Parish Name"] = $parishname
$resultObject["Year"] = $year
}
## And finally cast that hashtable to a PSCustomObject
$resultObject = [PSCustomObject] $resultObject
$results += $resultObject
}
$results | Export-Csv "$ParishName`_$year.csv" -NoTypeInformation -Force
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment