4

Sorry for limited knowledge with powershell. Here I try to read html content from a website, and output as csv file. Right now I can successful download whole html code with 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 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 what i really want is get the content and output to csv file like this:

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

What command can help me achieve this task?

3
  • I wonder if help Invoke-WebRequest -full would help? Commented Feb 26, 2014 at 8:02
  • This may help jdhitsolutions.com/blog/tag/invoke-webrequest Commented Feb 26, 2014 at 8:12
  • Thanks for your info, but i found out my powershell version is 2.0 Commented Feb 26, 2014 at 9:15

1 Answer 1

1

It was interresting for me too, thanks for the CA site. I wrote this on the corner of my desk, it needs improvments.

Here is a way using Html-Agility-Pack, in the following, I suppose that HtmlAgilityPack.dll is in Html-Agility-Pack directory of the directory script file.

# 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
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for you interest :) I download the "Html-Agility-Pack", there is many folders in it. Which one should i use? and where should I put it?
The answer of your question is in the Add-type : Add-Type -Path "Your Installation Directory\Html-Agility-Pack\HtmlAgilityPack.dll", but you can choose the one which best fit with the most recent .NET framework installed on your computer.
"You cannot call a method on a null-valued expression." + $trDatas = $Thetable.ChildNodes.Elements <<<< ("tr")
It means that $Thetable = $webDoc.DocumentNode.ChildNodes.Descendants('table') | where {$_.XPath -eq '/div[3]/div[1]/div[5]/table[1]/table[1]'} do not target the right table.
Thank you very much for your script. JPBlanc. I will study more about powershell. still don't get it, but i will figure it out! :D Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.