我正在尝试通过站点地图解析 XML,然后遍历地址以获取 Go 中帖子的详细信息。但是我收到了这个奇怪的错误:
: URL 中的第一个路径段不能包含冒号
这是代码片段:
type SitemapIndex struct {
Locations []Location `xml:"sitemap"`
}
type Location struct {
Loc string `xml:"loc"`
}
func (l Location) String() string {
return fmt.Sprintf(l.Loc)
}
func main() {
resp, _ := http.Get("https://www.washingtonpost.com/news-sitemaps/index.xml")
bytes, _ := ioutil.ReadAll(resp.Body)
var s SitemapIndex
xml.Unmarshal(bytes, &s)
for _, Location := range s.Locations {
fmt.Printf("Location: %s", Location.Loc)
resp, err := http.Get(Location.Loc)
fmt.Println("resp", resp)
fmt.Println("err", err)
}
}
输出:
Location:
https://www.washingtonpost.com/news-sitemaps/politics.xml
resp <nil>
err parse
https://www.washingtonpost.com/news-sitemaps/politics.xml
: first path segment in URL cannot contain colon
Location:
https://www.washingtonpost.com/news-sitemaps/opinions.xml
resp <nil>
err parse
https://www.washingtonpost.com/news-sitemaps/opinions.xml
: first path segment in URL cannot contain colon
...
...
我的猜测是Location.Loc在实际地址之前和之后返回一个新行。例如:\nLocation: https://www.washingtonpost.com/news-sitemaps/politics.xml\n
因为硬编码 URL 按预期工作:
for _, Location := range s.Locations {
fmt.Printf("Location: %s", Location.Loc)
test := "https://www.washingtonpost.com/news-sitemaps/politics.xml"
resp, err := http.Get(test)
fmt.Println("resp", resp)
fmt.Println("err", err)
}
但是我是 Go 的新手,所以我不知道出了什么问题。你能告诉我我哪里错了吗?
子衿沉夜
BIG阳
相关分类