2 回答

TA貢獻1828條經驗 獲得超3個贊
您確實是對的,問題來自換行符。如您所見,您在使用時Printf
沒有添加任何內容\n
,并且在輸出的開頭添加了一個,在輸出的結尾添加了一個。
您可以使用strings.Trim
刪除這些換行符。這是一個使用您嘗試解析的站點地圖的示例。修剪字符串后,您將能夠http.Get
毫無錯誤地調用它。
func main() {
? ? var s SitemapIndex
? ? xml.Unmarshal(bytes, &s)
? ? for _, Location := range s.Locations {
? ? ? ? loc := strings.Trim(Location.Loc, "\n")
? ? ? ? fmt.Printf("Location: %s\n", loc)
? ? }
}
如預期的那樣,此代碼正確輸出沒有任何換行符的位置:
Location: https://www.washingtonpost.com/news-sitemaps/politics.xml
Location: https://www.washingtonpost.com/news-sitemaps/opinions.xml
Location: https://www.washingtonpost.com/news-sitemaps/local.xml
Location: https://www.washingtonpost.com/news-sitemaps/sports.xml
Location: https://www.washingtonpost.com/news-sitemaps/national.xml
Location: https://www.washingtonpost.com/news-sitemaps/world.xml
Location: https://www.washingtonpost.com/news-sitemaps/business.xml
Location: https://www.washingtonpost.com/news-sitemaps/technology.xml
Location: https://www.washingtonpost.com/news-sitemaps/lifestyle.xml
Location: https://www.washingtonpost.com/news-sitemaps/entertainment.xml
Location: https://www.washingtonpost.com/news-sitemaps/goingoutguide.xml
字段中有這些換行符的原因Location.Loc是此 URL 返回的 XML。條目遵循這種形式:
<sitemap>
<loc>
https://www.washingtonpost.com/news-sitemaps/goingoutguide.xml
</loc>
</sitemap>
正如您所看到的,元素中的內容前后都有換行符loc。

TA貢獻1859條經驗 獲得超6個贊
查看修改代碼中嵌入的注釋以描述和修復問題
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 {
// Note that %v shows that there are indeed newlines at beginning and end of Location.Loc
fmt.Printf("Location: (%v)", Location.Loc)
// solution: use strings.TrimSpace to remove newlines from Location.Loc
resp, err := http.Get(strings.TrimSpace(Location.Loc))
fmt.Println("resp", resp)
fmt.Println("err", err)
}
}
- 2 回答
- 0 關注
- 242 瀏覽
添加回答
舉報