源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2021-12-19 15:18 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Powershell 获取特定的网页信息的代码
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格式的结果。 [img]http://files.jb51.net/file_images/article/201510/2015101011563615.jpg[/img] 我们可以用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   
  } 
[img]http://files.jb51.net/file_images/article/201510/2015101011563616.jpg[/img] 我也可以直接使用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 [img]http://files.jb51.net/file_images/article/201510/2015101011563617.jpg[/img] 功能很强大,使用却很简单。 本文出自 “麻婆豆腐” 博客
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部