设为首页 收藏本站
查看: 1013|回复: 0

[经验分享] yahoo OPEN api测试之weather feed

[复制链接]

尚未签到

发表于 2019-1-27 07:07:40 | 显示全部楼层 |阅读模式
  昨天写了个googlefeed api应用,突然对rss蛮感兴趣的,天生比较好奇,以前都是做windows程序感觉进入互联网领域,象是走进另外一扇大门,更加有趣,更加丰富多彩.先展示一下结果

调用feed api最主要要学习的知识是RSS读取的问题.
         .NET框架提供了System.Xml.Serialization.XmlSerializer类型,为将对象序列化为XML或将XML序列化为对象提供了很大支持。
         下面我们针对yahoo weather feed api,通过实例讲解一下如何读取RSS文件。
         首先通过yahoo weather feed api
         "http://xml.weather.yahoo.com/forecastrss?p=" & p & "&u=" & U
     可以得到如下RSS文件


         
                   Yahoo! Weather - Hangzhou, CH
                  http://us.rd.yahoo.com/dailynews/rss/weather/Hangzhou__CH/*http://weather.yahoo.com/forecast/CHXX0044_f.html
                   Yahoo! Weather for Hangzhou, CH
                   en-us
                   Fri, 27 Feb 2009 12:30 pm CST
                   60
                  
                  
                  
                  
                  
                  
                            Yahoo! Weather
                            142
                            18
                            http://weather.yahoo.com
                            http://l.yimg.com/a/i/us/nws/th/main_142b.gif
                  
                  
                            Conditions for Hangzhou, CH at 12:30 pm CST
                            30.23
                            120.17
                            http://us.rd.yahoo.com/dailynews/rss/weather/Hangzhou__CH/*http://weather.yahoo.com/forecast/CHXX0044_f.html
                            Fri, 27 Feb 2009 12:30 pm CST
                           
                           
                                    
                                    
                                     Current Conditions:
                                    
                                     Rain Shower, 43 F
                                    
                                     Forecast:
                                    
                                     Fri - Rain. High: 42 Low: 39
                                     Sat - Rain. High: 43 Low: 40
                                    
                                     Full Forecast at Yahoo! Weather
                                    
                                     (provided by The Weather Channel)
                                     ]]>
                           
                           
                           
                            CHXX0044_2009_02_27_12_30_CST
                  
         





如上文所说.NET框架提供了System.Xml.Serialization.XmlSerializer类型,现在我们要做的就是如何利用xmlSerializer将XML序列化为对象。
         对于上面的XML文件为了便于对象序列化我们会做如下替换
         
    str = str.Replace("yweather:", "")
        str = str.Replace("geo:", "")
        'Dim str1 As String = """  : "

        Dim str1 = "isPermaLink=""false"""
        str = str.Replace(str1, "")
        str1 = " version=""2.0"" xmlns:yweather=""http://xml.weather.yahoo.com/ns/rss/1.0"" xmlns:geo=""http://www.w3.org/2003/01/geo/wgs84_pos#"""
        str = str.Replace(str1, "")


接着我们需要按照xml的格式,将所有的节点定义为对象,并将节点的属性定义为对象的属性。
         如该RSS中需要先定义rss对象
         
Imports Microsoft.VisualBasic
Imports System.Xml.Serialization
Partial Class YahooWeatherForecast

     _
       Public Class rssClass
        Private _channel As channelClass

         _
        Public Property channel() As channelClass
            Get
                Return _channel
            End Get
            Set(ByVal value As channelClass)
                _channel = value
            End Set
        End Property
    End Class
End Class

在该对象中包含chanelClass,保持和xml的文件相同的结构
接下来我们需要定义chanelClass,代码如下:
Imports Microsoft.VisualBasic
Imports System.Xml.Serialization
Partial Class YahooWeatherForecast
    Partial Class rssClass
        Partial Class channelClass
            Private _title As String
            '''
            '''  The title of the feed, which includes the location city. For example "Yahoo! Weather - Sunnyvale, CA"
            '''
             _
            Public Property title() As String
                Get
                    Return _title
                End Get
                Set(ByVal value As String)
                    _title = value
                End Set
            End Property


            Private _link As String
            '''
            '''  The URL for the Yahoo! Weather page of the forecast for this location. For example http://us.rd.yahoo.com/dailynews/rss/weather/ Sunnyvale__CA/ *http://xml.weather.yahoo.com/ forecast/94089_f.html
            '''
             _
            Public Property link() As String
                Get
                    Return _link
                End Get
                Set(ByVal value As String)
                    _link = value
                End Set
            End Property

            Private _language As String
            '''
            '''  The language of the weather forecast, for example, en-us for US English.
            '''
             _
            Public Property language() As String
                Get
                    Return _language
                End Get
                Set(ByVal value As String)
                    _language = value
                End Set
            End Property

            Private _description As String
            '''
            '''  The overall description of the feed including the location, for example "Yahoo! Weather for Sunnyvale, CA"
            '''
             _
            Public Property description() As String
                Get
                    Return _description
                End Get
                Set(ByVal value As String)
                    _description = value
                End Set
            End Property

            Private _lastBuildDate As String
            '''
            '''  The last time the feed was updated. The format is in the date format defined by RFC822 Section 5, for example Mon, 256 Sep 17:25:18 -0700.
            '''
             _
            Public Property lastBuildDate() As String
                Get
                    Return _lastBuildDate
                End Get
                Set(ByVal value As String)
                    _lastBuildDate = value
                End Set
            End Property

            Private _ttl As String
            '''
            '''  Time to Live; how long in minutes this feed should be cached.
            '''
             _
            Public Property ttl() As String
                Get
                    Return _ttl
                End Get
                Set(ByVal value As String)
                    _ttl = value
                End Set
            End Property

            Private _location As locationClass
            '''
            '''  The location of this forecast.
            '''
             _
            Public Property location() As locationClass
                Get
                    Return _location
                End Get
                Set(ByVal value As locationClass)
                    _location = value
                End Set
            End Property

            Private _units As unitsClass
            '''
            '''  Units for various aspects of the forecast.
            '''
             _
            Public Property units() As unitsClass
                Get
                    Return _units
                End Get
                Set(ByVal value As unitsClass)
                    _units = value
                End Set
            End Property

            Private _wind As windClass
            '''
            '''  Forecast information about wind.
            '''
             _
            Public Property wind() As windClass
                Get
                    Return _wind
                End Get
                Set(ByVal value As windClass)
                    _wind = value
                End Set
            End Property

            Private _atmosphere As atmosphereClass
            '''
            '''  Forecast information about current atmospheric pressure, humidity, and visibility.
            '''
             _
            Public Property atmosphere() As atmosphereClass
                Get
                    Return _atmosphere
                End Get
                Set(ByVal value As atmosphereClass)
                    _atmosphere = value
                End Set
            End Property

            Private _astronomy As astronomyClass
            '''
            '''  Forecast information about current astronomical conditions.
            '''
             _
            Public Property astronomy() As astronomyClass
                Get
                    Return _astronomy
                End Get
                Set(ByVal value As astronomyClass)
                    _astronomy = value
                End Set
            End Property
            Private _image As imageClass
            '''
            '''  The image used to identify this feed
            '''
             _
            Public Property image() As imageClass
                Get
                    Return _image
                End Get
                Set(ByVal value As imageClass)
                    _image = value
                End Set
            End Property
            Private _item As itemClass
            '''
            '''  The local weather conditions and forecast for a specific location.
            '''
             _
            Public Property item() As itemClass
                Get
                    Return _item
                End Get
                Set(ByVal value As itemClass)
                    _item = value
                End Set
            End Property
        End Class
    End Class
End Class


在chanel中声明了他包含哪些子节点,同样我们也需要为这些子节点申明对象。
Imports Microsoft.VisualBasic
Imports System.Xml.Serialization
Partial Class YahooWeatherForecast
    Partial Class rssClass
        Partial Class channelClass
            Public Class atmosphereClass
                Private _humidity As Integer
                '''
                '''  humidity, in percent
                '''
                 _
                Public Property humidity() As Integer
                    Get
                        Return _humidity
                    End Get
                    Set(ByVal value As Integer)
                        _humidity = value
                    End Set
                End Property
                Private _visibility As Double
                '''
                '''   in the units specified by the distance attribute of the units element (mi or km). Note that the visibility is specified as the actual value * 100. For example, a visibility of 16.5 miles will be specified as 1650. A visibility of 14 kilometers will appear as 1400.
                '''
                 _
                Public Property visibility() As Double
                    Get
                        Return _visibility
                    End Get
                    Set(ByVal value As Double)
                        _visibility = value
                    End Set
                End Property

                Private _pressure As Double
                '''
                '''  barometric pressure, in the units specified by the pressure attribute of the units element (in or mb).
                '''
                 _
                Public Property pressure() As Double
                    Get
                        Return _pressure
                    End Get
                    Set(ByVal value As Double)
                        _pressure = value
                    End Set
                End Property


                Private _rising As Integer
                '''
                '''  state of the barometric pressure,   steady = 0, rising = 1, falling = 2
                '''
                 _
                Public Property rising() As Integer
                    Get
                        Return _rising
                    End Get
                    Set(ByVal value As Integer)
                        _rising = value
                    End Set
                End Property
            End Class
        End Class
    End Class
End Class

在申明完对象之后,我们就可以将xml文件转换为相应的对象。
代码如下:
      Dim doc As New System.Xml.XmlDocument()
        Dim str As String = "http://xml.weather.yahoo.com/forecastrss?p=" & p & "&u=" & U
        Dim c As rssClass
        'Load data   
        doc.Load(str)
        str = doc.OuterXml


        'XmlSerializer could not Serialize XML with this Elements so i just replace them with Null String!
        str = str.Replace("yweather:", "")
        str = str.Replace("geo:", "")
        'Dim str1 As String = """  : "

        Dim str1 = "isPermaLink=""false"""
        str = str.Replace(str1, "")
        str1 = " version=""2.0"" xmlns:yweather=""http://xml.weather.yahoo.com/ns/rss/1.0"" xmlns:geo=""http://www.w3.org/2003/01/geo/wgs84_pos#"""
        str = str.Replace(str1, "")
        Dim memoryStream As New IO.MemoryStream()
        Dim d As New IO.StringReader(str)

        Dim xs As New XmlSerializer(GetType(rssClass))

        c = CType(xs.Deserialize(d), rssClass)

        rss = c


最后用户可以根据自己的需要将对象中的属性取出,使用于相关的应用中。类似如下代码
       Dim t As New YahooWeatherLib.YahooWeatherForecast("CHXX0044", "f")
        Label1.Text = t.rss.channel.item.forecast.high
        Label2.Text = t.rss.channel.item.forecast.low
        Label5.Text = t.rss.channel.item.title





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-667990-1-1.html 上篇帖子: kali rolling 下安装 open *** 下篇帖子: Too many open files错误
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表