当前位置: 移动技术网 > IT编程>脚本编程>Shell > Powershell 获取特定的网页信息的代码

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

2017年12月01日  | 移动技术网IT编程  | 我要评论
powershell可以很轻松的获取网页的信息并读取到对应的内容。如果对象的格式是xml或者json,那就更容易处理了,一般经常使用invoke-restmethod和in

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格式的结果。

wkiol1ytzcayhgafaaerc9ji2ja626.jpg

我们可以用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   
  } 

wkiol1ytzjswgdo5aaa5n_hx8fc325.jpg

我也可以直接使用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

wkiol1ytal_byqbcaasvftkgnjq514.jpg

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

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网