使用powershell读取html内容

使用powershell读取html内容,第1张

概述很抱歉,对Power Shell的了解有限.在这里,我尝试从网站上读取html内容,并输出为csv文件.现在我可以使用我的powershell脚本成功下载整个html代码: $url = "http://cloudmonitor.ca.com/en/ping.php?vtt=1392966369&varghost=www.yahoo.com&vhost=_&vaction=ping&ping=st 很抱歉,对Power Shell的了解有限.在这里,我尝试从网站上读取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 >  <td >Stockholm,Sweden (sesto01):</td>  <td ><span ID="cp20">Okay</span>  </td>  <td ><span ID="minrtt20">21.8</span>  </td>  <td ><span ID="avgrtt20">21.8</span>  </td>  <td ><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.9,2a00:1288:f00e:1fe::3001........

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

解决方法 感谢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}
总结

以上是内存溢出为你收集整理的使用powershell读取html内容全部内容,希望文章能够帮你解决使用powershell读取html内容所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1055224.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存