Advertisement
Pug_coder

RSS-parse

Sep 17th, 2021
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.22 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "encoding/xml"
  5.     "fmt"
  6.     "net/http"
  7. )
  8.  
  9. type Enclosure struct {
  10.     Url    string `xml:"url,attr"`
  11.     Length int64  `xml:"length,attr"`
  12.     Type   string `xml:"type,attr"`
  13. }
  14.  
  15. type Item struct {
  16.     Title     string    `xml:"title"`
  17.     Link      string    `xml:"link"`
  18.     Desc      string    `xml:"description"`
  19.     Guid      string    `xml:"guid"`
  20.     Enclosure Enclosure `xml:"enclosure"`
  21.     PubDate   string    `xml:"pubDate"`
  22. }
  23.  
  24. type Channel struct {
  25.     Title string `xml:"title"`
  26.     Link  string `xml:"link"`
  27.     Desc  string `xml:"description"`
  28.     Items []Item `xml:"item"`
  29. }
  30.  
  31. type Rss struct {
  32.     Channel Channel `xml:"channel"`
  33. }
  34.  
  35. func main() {
  36.     resp, err := http.Get("http://blagnews.ru/rss_vk.xml")
  37.     if err != nil {
  38.         fmt.Printf("Error GET: %v\n", err)
  39.         return
  40.     }
  41.     defer resp.Body.Close()
  42.  
  43.     rss := Rss{}
  44.  
  45.     decoder := xml.NewDecoder(resp.Body)
  46.     err = decoder.Decode(&rss)
  47.     if err != nil {
  48.         fmt.Printf("Error Decode: %v\n", err)
  49.         return
  50.     }
  51.  
  52.     fmt.Printf("Channel title: %v\n", rss.Channel.Title)
  53.     fmt.Printf("Channel link: %v\n", rss.Channel.Link)
  54.  
  55.     for i, item := range rss.Channel.Items {
  56.         fmt.Printf("%v. item title: %v\n", i, item.Title)
  57.         fmt.Printf("PubDate : %v\n",  item.PubDate)
  58.  
  59.     }
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement