【问题标题】:Using powershell to read html content使用powershell读取html内容
【发布时间】:2014-03-28 19:51:59
【问题描述】:

抱歉,Powershell 知识有限。在这里,我尝试从网站读取 html 内容,并输出为 csv 文件。现在我可以使用我的 powershell 脚本成功下载整个 html 代码:

$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()

获取html代码,类似这样:

  ........
  <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>
  ........

但我真正想要的是像这样获取内容并输出到 csv 文件:

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

什么命令可以帮助我完成这个任务?

【问题讨论】:

标签: html powershell csv powershell-2.0


【解决方案1】:

这对我来说也很有趣,感谢 CA 网站。这是我写在桌角的,需要改进。

这里是使用Html-Agility-Pack的一种方式,下面我假设HtmlAgilityPack.dll在目录脚本文件的Html-Agility-Pack目录下。

# 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
}

【讨论】:

  • 感谢您的关注 :) 我下载了“Html-Agility-Pack”,里面有很多文件夹。我应该使用哪一个?我应该把它放在哪里?
  • 您的问题的答案在 Add-type 中:Add-Type -Path "Your Installation Directory\Html-Agility-Pack\HtmlAgilityPack.dll",但您可以选择最适合您计算机上安装的最新 .NET 框架的一种。
  • "您不能在空值表达式上调用方法。" + $trDatas = $Thetable.ChildNodes.Elements
  • 表示$Thetable = $webDoc.DocumentNode.ChildNodes.Descendants('table') | where {$_.XPath -eq '/div[3]/div[1]/div[5]/table[1]/table[1]'}没有定位到正确的表。
  • 非常感谢您的脚本。摩根大通。我将更多地研究powershell。仍然不明白,但我会弄清楚的! :D 再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-03
  • 2010-11-02
  • 2015-01-16
  • 2014-12-15
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
相关资源
最近更新 更多