Powershell 获取特定的网页信息的代码

2023-12-05 0 712

Powershell可以很轻松的获取网页的信息并读取到对应的内容。如果对象的格式是XML或者Json,那就更容易处理了,一般经常使用invoke-restmethod和invoke-webrequest这两个命令。前者主要是获取Json格式的内容,后者可以获取整个网页的内容。

比如说我希望查询明天悉尼的天气如何。网上随便搜了一个提供API的站点
http://openweathermap.org/current#name

我打算搜索悉尼的,那么对应的格式是
http://api.openweathermap.org/data/2.5/weather?q=sydney,au他会自动生成一个Json格式的结果。

Powershell 获取特定的网页信息的代码

我们可以用invoke-restmethod直接获取这个结果,比如说

$b=invoke-restmethod \”http://api.openweathermap.org/data/2.5/weather?q=sydney,au\”
$c=[pscustomobject]@{
\’Description\’=$b.weather.description
\’name\’=$b.name
\’windspeed\’=$b.wind.speed
}

Powershell 获取特定的网页信息的代码

我也可以直接使用invoke-webrequest抓取整个网页的内容,然后从Json的格式转换过来也是一样的

$a= Invoke-WebRequest -Uri \”http://api.openweathermap.org/data/2.5/weather?q=sydney,au\”$b=$a.Content | ConvertFrom-Json

类似的,如果我想获取一个博客的RSS的最新内容。可以使用invoke-webrequest抓取对应的XML文件,比如

[xml]$a= Invoke-WebRequest -Uri \”http://blogs.msdn.com/b/powershell/rss.aspx“$a.rss.channel.Item | select title,pubdate

Powershell 获取特定的网页信息的代码

功能很强大,使用却很简单。

本文出自 “麻婆豆腐” 博客

您可能感兴趣的文章:

  • Powershell 查询 Windows 日志的方法
  • Powershell 查找用户的主SMTP地址
  • powershell解决win10开始菜单和通知中心无法打开
  • Powershell错误处理之what-if
  • PowerShell 4.0实现自动化设置服务器
  • 揭秘PowerShell 5.0新特性和新功能
  • Windows 8 中的 PowerShell 3.0
  • PowerShell使用小技巧分享
  • 使用PowerShell修改注册表
  • PowerShell用户认证Function实例代码

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 PowerShell Powershell 获取特定的网页信息的代码 https://www.u-9.cn/jiaoben/powershell/101079.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务