【go】RSS解析
```package main
import (
"bytes"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)
func main() {
srcName := "i.rss"
if len(os.Args) >= 2 {
srcName = os.Args
}
xml_up := getXmlFile("i.rss")
xml := getXmlFile(srcName)
rss_up := ParseRSS(xml_up)
rss := ParseRSS(xml)
displayRssChannel(rss.Channel)
ComparedRSS(rss_up, rss)
}
//对比两张rss
func ComparedRSS(rss_up *RssFeed, rss *RssFeed) string {
var items_up []*RssItem = rss_up.Channel.Item
var items []*RssItem = rss.Channel.Item
var maxLen = 10
var buffer bytes.Buffer
for i := 0; i < len(items); i++ {
var check = false
//判断在数组中是否存在
for j := 0; j < len(items_up); j++ {
if items.Title == items_up.Title {
check = true
break
}
}
if !check {
fmt.Println(items.Title)
fmt.Println("bbs.yzjlb.net" + items.Comments)
buffer.WriteString(fmt.Sprintf("%s\n%s%s", items.Title, "***.net", items.Comments))
buffer.WriteString("\n\n")
}
if i >= maxLen {
break
}
// var datetime, err = time.Parse(" 21 January 2021 13:01:42 +0800", rss.Item.PubDate.PubDate, ",")+1:])
// // var datetime, err = time.ParseDuration(rss.Item.PubDate)
// if err != nil {
//fmt.Println("err: ", err)
// }
// fmt.Println(datetime.Format("2006-01-02 15:04:05 010"))
}
if buffer.Len() > 2 {
return buffer.String()
}
return ""
}
//显示rss信息
func displayRssChannel(rss *RssChannel) {
fmt.Println("Title:", rss.Title)
fmt.Println("Link:", rss.Link)
fmt.Println("Last build date:", rss.LastBuildDate)
fmt.Println("Pub date:", rss.PubDate)
fmt.Println("Language:", rss.Language)
fmt.Println("Description:", rss.Description)
for i := 0; i < len(rss.Item); i++ {
fmt.Println(rss.Item.Title)
// var datetime, err = time.Parse(" 21 January 2021 13:01:42 +0800", rss.Item.PubDate.PubDate, ",")+1:])
// // var datetime, err = time.ParseDuration(rss.Item.PubDate)
// if err != nil {
//fmt.Println("err: ", err)
// }
// fmt.Println(datetime.Format("2006-01-02 15:04:05 010"))
}
}
//读取文件
func getXmlFile(filename string) []byte {
f, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println("Unable to read src xml file.", err)
os.Exit(0)
}
return f
}
//将rss数据解析为RssFeed结构体
func ParseRSS(x []byte) *RssFeed {
feed := RssFeed{}
d := xml.NewDecoder(bytes.NewReader(x))
err := d.Decode(&feed)
if err != nil {
fmt.Println("Failed decoding xml")
os.Exit(0)
}
return &feed
}
type RssFeed struct {
XMLName xml.Name `xml:"rss"`
Channel *RssChannel `xml:"channel"`
}
type RssChannel struct {
XMLName xml.Name `xml:"channel"`
Title string `xml:"title"`
Description string `xml:"description"`
Link string `xml:"link"`
Language string `xml:"language"`
PubDate string `xml:"pubDate"`
LastBuildDate string `xml:"lastBuildDate"`
Item []*RssItem `xml:"item"`
}
type RssItem struct {
XMLName xml.Name `xml:"item"`
Title string `xml:"title"`
Link string `xml:"link"`
PubDate string `xml:"pubDate"`
Creator string `xml:"dc:creator"`
Description string `xml:"description"`
Comments string `xml:"comments"`
}
``` 证书还是啥,时间地点人物起因经过结果,描述一下嘛 http://c2cpicdw.qpic.cn/offpic_new/2541012655//2541012655-3008580082-23438B319104B92D9B67822BC539650F/0?term=3
测试一下图片。。 又学到一点,go的xml和结构体转换。
页:
[1]