Powershell 獲取特定的網(wǎng)頁信息的代碼
Powershell可以很輕松的獲取網(wǎng)頁的信息并讀取到對應(yīng)的內(nèi)容。如果對象的格式是XML或者Json,那就更容易處理了,一般經(jīng)常使用invoke-restmethod和invoke-webrequest這兩個命令。前者主要是獲取Json格式的內(nèi)容,后者可以獲取整個網(wǎng)頁的內(nèi)容。
比如說我希望查詢明天悉尼的天氣如何。網(wǎng)上隨便搜了一個提供API的站點(diǎn)
http://openweathermap.org/current#name
我打算搜索悉尼的,那么對應(yīng)的格式是
http://api.openweathermap.org/data/2.5/weather?q=sydney,au他會自動生成一個Json格式的結(jié)果。
我們可以用invoke-restmethod直接獲取這個結(jié)果,比如說
$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 }
我也可以直接使用invoke-webrequest抓取整個網(wǎng)頁的內(nèi)容,然后從Json的格式轉(zhuǎn)換過來也是一樣的
$a= Invoke-WebRequest -Uri "http://api.openweathermap.org/data/2.5/weather?q=sydney,au"$b=$a.Content | ConvertFrom-Json
類似的,如果我想獲取一個博客的RSS的最新內(nèi)容??梢允褂胕nvoke-webrequest抓取對應(yīng)的XML文件,比如
[xml]$a= Invoke-WebRequest -Uri "http://blogs.msdn.com/b/powershell/rss.aspx“$a.rss.channel.Item | select title,pubdate
功能很強(qiáng)大,使用卻很簡單。
本文出自 “麻婆豆腐” 博客
相關(guān)文章
PowerShell使用Remove-Item命令刪除文件、注冊表項(xiàng)介紹
這篇文章主要介紹了PowerShell使用Remove-Item命令刪除文件、注冊表項(xiàng)介紹,另外Remove-Item的別名也有很多,需要的朋友可以參考下2014-08-08Windows PowerShell是啥?看完本文你就懂它了
這篇文章主要介紹了Windows PowerShell是啥?Windows PowerShell是什么?Windows PowerShell有哪些特性?Windows PowerShell有什么用?看完本文你就懂它了,需要的朋友可以參考下2015-04-04Windows Powershell ForEach-Object 循環(huán)
Powershell管道就像流水線,對于數(shù)據(jù)的處理是一個環(huán)節(jié)接著一個環(huán)節(jié),如果你想在某一環(huán)節(jié)對流進(jìn)來的數(shù)據(jù)逐個細(xì)致化的處理,可是使用ForEach-Object,$_ 代表當(dāng)前的數(shù)據(jù)。2014-10-10