在Go中解析RSS feed

问题描述 投票:0回答:2

我正在尝试用Go编写播客下载器。以下代码解析RSS提要,但将解析的数据打印到标准输出时,通道的链接为空。我不知道为什么有什么建议么?我是新手。

package main

import (
    "encoding/xml"
    "fmt"
    "net/http"
)

type Enclosure struct {
    Url    string `xml:"url,attr"`
    Length int64  `xml:"length,attr"`
    Type   string `xml:"type,attr"`
}

type Item struct {
    Title     string    `xml:"title"`
    Link      string    `xml:"link"`
    Desc      string    `xml:"description"`
    Guid      string    `xml:"guid"`
    Enclosure Enclosure `xml:"enclosure"`
    PubDate   string    `xml:"pubDate"`
}

type Channel struct {
    Title string `xml:"title"`
    Link  string `xml:"link"`
    Desc  string `xml:"description"`
    Items []Item `xml:"item"`
}

type Rss struct {
    Channel Channel `xml:"channel"`
}

func main() {
    resp, err := http.Get("http://www.bbc.co.uk/programmes/p02nrvz8/episodes/downloads.rss")
    if err != nil {
        fmt.Printf("Error GET: %v\n", err)
        return
    }
    defer resp.Body.Close()

    rss := Rss{}

    decoder := xml.NewDecoder(resp.Body)
    err = decoder.Decode(&rss)
    if err != nil {
        fmt.Printf("Error Decode: %v\n", err)
        return
    }

    fmt.Printf("Channel title: %v\n", rss.Channel.Title)
    fmt.Printf("Channel link: %v\n", rss.Channel.Link)

    for i, item := range rss.Channel.Items {
        fmt.Printf("%v. item title: %v\n", i, item.Title)
    }
}
xml go rss
2个回答
4
投票

来自rss feed的xml具有一个channel元素,其中包含两个子“ link”元素:“ link”和“ atom:link”。即使有名称空间前缀,Go xml unmarshaller也会看到冲突。另请参见local name collisions failissue on github

<?xml version="1.0" encoding="UTF-8"?>
...
   <channel>
      <title>Forum - Sixty Second Idea to Improve the World</title>
      <link>http://www.bbc.co.uk/programmes/p02nrvz8</link>
      ...
      <atom:link href="http://www.bbc.co.uk/..." />

0
投票

或使用go-rss这样的库或informado这样的工具来读取各种RSS feed。

© www.soinside.com 2019 - 2024. All rights reserved.