如何在 VB.net 中获得每小时天气预报?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16377296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:29:50  来源:igfitidea点击:

How can I get hourly weather forecast in VB.net?

vb.netweather-api

提问by ZeverMX

I've seen some questions similar to this on here, but none of them seem to help me. I don't really care what API is used, be it Google, Yahoo!, The Weather Channel, or any other. I've got code that will get the high and low of the current day, based on the location given by the user, but I can't seem to get the hourly predictions for temperature or weather condition, which is what I'm really looking for. I don't really care for wind speeds, humidity, or the "feels like" temperature, though I'll add them if I can figure out how to. I'm trying to get data that will look something like what is here. (www.weather.com/...)

我在这里看到了一些与此类似的问题,但似乎没有一个对我有帮助。我并不真正关心使用的是什么 API,无论是 Google、Yahoo!、The Weather Channel 还是其他任何 API。我有代码可以根据用户提供的位置获取当天的高低,但我似乎无法获得每小时温度或天气状况的预测,这就是我真正想要的寻找。我并不真正关心风速、湿度或“感觉”温度,但如果我能弄清楚如何添加它们,我会添加它们。我正在尝试获取看起来像这里的数据。(www.weather.com/...)

I'm pretty new to parsing XML so that may be part of my problem, too. Thanks in advance for any help. I greatly appreciate it.

我对解析 XML 还很陌生,所以这也可能是我的问题的一部分。在此先感谢您的帮助。我非常感谢。

采纳答案by ZeverMX

I finally found what I was looking for from Weather Underground. They have three APIs, and, starting with the middle one, gives hourly forecasts for 36 hours. You get English and Metric predicted statistics for temperature, "feels like" temperature, dew point, wind speed, and wind direction. There's also wind chill and heat index, but every result for both in English and Metric was -9998, so I'm pretty sure those results are a bit off! Also there are qpf, snow, pop, and mslp. Unfortunately qpf and snow have not results and I'm not sure what pop and mslp are. All results are available in JSON and XML. They have some image requests, too, but I haven't looked into them yet.

我终于从Weather Underground找到了我想要的东西。他们有三个 API,从中间的一个开始,给出 36 小时的每小时预测。您可以获得温度、“感觉”温度、露点、风速和风向的英制和公制预测统计数据。还有风寒和热指数,但英制和公制的每个结果都是 -9998,所以我很确定这些结果有点偏差!还有 qpf、snow、pop 和 mslp。不幸的是 qpf 和 snow 没有结果,我不确定 pop 和 mslp 是什么。所有结果都以 JSON 和 XML 格式提供。他们也有一些图像请求,但我还没有研究它们。

Update:
Link Updated

更新:
链接已更新

回答by OneFineDay

I have something you might enjoy:

我有一些你可能会喜欢的东西:

<Runtime.CompilerServices.Extension()>
Module Weather

Public Structure WeatherInfo_Forecast
  Dim DayOfWeek As String
  Dim low As Double
  Dim high As Double
  Dim icon As String
End Structure

Public Structure WeatherInfo_Wind
  Dim direction As String
  Dim speed As Double
  Dim unit As String
End Structure

Public Structure WeatherInfo_Typed
  Dim Failed As Boolean
  Dim errormessage As Exception
  Dim location As String
  Dim forcast_date As DateTime
  Dim checked_time_date As DateTime
  Dim humidity As Double
  Dim highf As Double
  Dim lowf As Double
  Dim highc As Double
  Dim lowc As Double
  Dim currenttempC As Double
  Dim currenttempF As Double
  Dim predicted_icon As String
  Dim current_icon As String
  Dim current_condition As String
  Dim predicted_condition As IEnumerable(Of WeatherInfo_Forecast)
  Dim wind_condition As WeatherInfo_Wind
  Dim day As String
End Structure

<Runtime.CompilerServices.Extension()> _
Public Function ToC(ByVal F As Double) As Double
  Return ((F - 32) / 9) * 5
End Function

<Runtime.CompilerServices.Extension()> _
Public Function TryParseAsDouble(ByVal s As String) As Double
  Dim rv As Double
  If Double.TryParse(s, rv) = False Then rv = Double.NaN
  Return rv
End Function

<Runtime.CompilerServices.Extension()> _
Public Function TryParseAsDate(ByVal s As String) As DateTime
  Dim rv As DateTime
  If DateTime.TryParse(s, rv) = False Then rv = Nothing
  Return rv
End Function

Private Function ParseHumidity(ByVal s As String) As Double
  If Not s Is Nothing Then
  Dim humRegEx As New System.Text.RegularExpressions.Regex("Humidity: (?<Value>\d+)\w*\%")
  Dim m = humRegEx.Match(s)
  If m.Length = 0 Then Return Double.NaN
    Return Double.Parse(m.Groups("Value").Value)
  End If
End Function

Private Function ParseWind(ByVal s As String) As WeatherInfo_Wind
  Dim rv As New WeatherInfo_Wind
  If Not s Is Nothing Then
    Dim humRegEx As New System.Text.RegularExpressions.Regex("Wind\:\s+(?<Direction>[NEWSnews]{1,2})\s+at\s+(?<speed>(?<value>\d+)\s(?<units>\w+)){1}")
    Dim m = humRegEx.Match(s)
    rv.speed = Double.NaN
    If m.Length = 0 Then Return rv
    With rv
     .direction = m.Groups("Direction").Value
     If Double.TryParse(m.Groups("value").Value, .speed) = False Then .speed = Double.NaN
     .unit = m.Groups("units").Value
    End With
  End If
  Return rv
End Function

<DebuggerHidden()>
Public Function Grab_Weather(ByVal Location As String) As WeatherInfo_Typed
  Dim GrabWeather As New WeatherInfo_Typed
  With GrabWeather
  .Failed = True
  Try
    Dim xml As XDocument = XDocument.Load("http://www.google.com/ig/api?weather=" & Location)
    Dim xp = xml.<problem_cause>
    If xp.Any Then Return GrabWeather
    .location = xml...<city>.@data
    .forcast_date = xml...<forecast_date>[email protected]
    .checked_time_date = xml...<current_date_time>[email protected]
    .humidity = ParseHumidity(xml...<humidity>.@data)
    .highf = xml...<high>[email protected]
    .lowf = xml...<low>[email protected]
    .highc = GrabWeather.highf.ToC
    .lowc = GrabWeather.highc.ToC
    .currenttempC = xml...<temp_c>[email protected]
    .currenttempF = xml...<temp_f>[email protected]
    '.current_icon = "http://www.google.com" & xml...<icon>.@data
    '.predicted_icon = "http://www.google.com" & xml...<high>.@data
    .current_condition = xml...<condition>.@data
    .predicted_condition = From f In xml...<forecast_conditions> _
                        Select New WeatherInfo_Forecast With { _
                          .DayOfWeek = f.<day_of_week>.Value, _
                          .high = f.<high>[email protected], _
                          .low = f.<low>[email protected]}
    '.icon = "http://www.google.com" & f.<icon>.@data}
    .wind_condition = ParseWind(xml...<wind_condition>.@data)
    .day = xml...<day_of_week>.@data
    .Failed = False
    Return GrabWeather
  Catch ex As Exception
    .errormessage = ex
    Return GrabWeather
  End Try
  End With
End Function
End Module