返回列表 发帖

[转载代码] PowerShell查询天气

最近全国雨水较多,写了个查询天气的小脚本,利用google提供的api。
$city=Read-Host "请输入要查询城市字母全拼"
$URI = "http://www.google.com/ig/api?hl=zh-cn&weather=$city"
#建立WebClient对象
$webclient = new-object net.webclient
#获取XML
try{
    $proxy = [xml]$webclient.downloadstring($URI)
}
catch [Exception]{
    "连接失败"
    return
}
if( $Proxy.xml_api_reply.weather.problem_cause){
    "没有该城市的天气信息或输入了错误的城市名"
    return
}
#获取温度
$Temp = $Proxy.xml_api_reply.weather.current_conditions.temp_c.GetAttribute("data")
#获取天气
$Condition = $Proxy.xml_api_reply.weather.current_conditions.Condition.GetAttribute("data")
#获取湿度
$humidity = $Proxy.xml_api_reply.weather.current_conditions.humidity.GetAttribute("data")
#获取风力
$wind= $Proxy.xml_api_reply.weather.current_conditions.wind_condition.GetAttribute("data")
$Forecast=$null
$Proxy.xml_api_reply.weather.selectNodes("//forecast_conditions") | ForEach-Object {
    $Forecast+= $_.day_of_week.GetAttribute("data") + " "
    $Forecast += $_.low.GetAttribute("data") + " - " + $_.high.GetAttribute("data")
    $Forecast += " " + $_.condition.GetAttribute("data") + "`n"
}
Write-Host "========当前天气情况=======
城市: $city
温度: $Temp
天气: $Condition
$humidity
$wind
$Forecast"
输出的结果1
========当前天气情况=======
城市: beijing
温度: 25
天气: 雨
湿度: 94%
风向: 东、风速:3 米/秒
周三 22 - 29 可能有雨
周四 24 - 31 雷阵雨
周五 24 - 31 可能有暴风雨
周六 22 - 28 可能有暴风雨
输出的结果2
========当前天气情况=======
城市: wuhan
温度: 32
天气: 晴
湿度: 62%
风向: 东南、风速:5 米/秒
周三 26 - 35 可能有暴风雨
周四 25 - 35
周五 26 - 35 可能有暴风雨
周六 26 - 35 可能有暴风雨COPY
http://hi.baidu.com/shrekzz/item/58ca56f01537a01dcf9f325d
1

评分人数

    • PowerShell: 感谢分享,powershell拽web服务之一.PB + 2

返回列表