Using powershell to read html content

Sorry for the limited knowledge with powershell. Here I am trying to read html content from a website and output it as a csv file. Right now, I can successfully download all the html code using my powershell script:

$url = "http://cloudmonitor.ca.com/en/ping.php?vtt=1392966369&varghost=www.yahoo.com&vhost=_&vaction=ping&ping=start";
$Path = "$env:userprofile\Desktop\test.txt"

$ie = New-Object -com InternetExplorer.Application 
$ie.visible = $true
$ie.navigate($url)

while($ie.ReadyState -ne 4) { start-sleep -s 10 }

#$ie.Document.Body.InnerText | Out-File -FilePath $Path
$ie.Document.Body | Out-File -FilePath $Path
$ie.Quit()

Get the html code, something like this:

  ........
  <tr class="light-grey-bg">
  <td class="right-dotted-border">Stockholm, Sweden (sesto01):</td>
  <td class="right-dotted-border"><span id="cp20">Okay</span>
  </td>
  <td class="right-dotted-border"><span id="minrtt20">21.8</span>
  </td>
  <td class="right-dotted-border"><span id="avgrtt20">21.8</span>
  </td>
  <td class="right-dotted-border"><span id="maxrtt20">21.9</span>
  </td>
  <td><span id="ip20">2a00:1288:f00e:1fe::3001</span>
  </td>
  </tr>
  ........

But I really want to get the content and output to the csv file as follows:

Stockholm Sweden (sesto01),Okay,21.8,21.8,21.9,2a00:1288:f00e:1fe::3001
........

Which team can help me complete this task?

+4
source share
1 answer

I was also interested, thanks for the CA site. I wrote this on the corner of my desk, he needs improvements.

Html-Agility-Pack, , HtmlAgilityPack.dll Html- Agility-Pack script.

# PingFromTheCloud.ps1

$url = "http://cloudmonitor.ca.com/en/ping.php?vtt=1392966369&varghost=www.silogix.fr&vhost=_&vaction=ping&ping=start";
$Path = "c:\temp\Pingtest.htm"

$ie = New-Object -com InternetExplorer.Application 
$ie.visible = $true
$ie.navigate($url)

while($ie.ReadyState -ne 4) { start-sleep -s 10 }

#$ie.Document.Body.InnerText | Out-File -FilePath $Path
$ie.Document.Body | Out-File -FilePath $Path
$ie.Quit()

Add-Type -Path "$(Split-Path -parent $PSCommandPath)\Html-Agility-Pack\HtmlAgilityPack.dll"


$webGraber = New-Object -TypeName HtmlAgilityPack.HtmlWeb
$webDoc = $webGraber.Load("c:\temp\Pingtest.htm")
$Thetable = $webDoc.DocumentNode.ChildNodes.Descendants('table') | where {$_.XPath -eq '/div[3]/div[1]/div[5]/table[1]/table[1]'}

$trDatas = $Thetable.ChildNodes.Elements("tr")

Remove-Item "c:\temp\Pingtest.csv"

foreach ($trData in $trDatas)
{
  $tdDatas = $trData.elements("td")
  $line = ""
  foreach ($tdData in $tdDatas)
  {
    $line = $line + $tdData.InnerText.Trim() + ','
  }
  $line.Remove($line.Length -1) | Out-File -FilePath "c:\temp\Pingtest.csv" -Append
}
+1

Source: https://habr.com/ru/post/1528900/


All Articles