我正在解析包含 URL 的 XML,并且我想迭代此 XML 以获取所有 URL 并向每个 URL 发出请求,但字符串包含换行符\n。如何避免 URL 中出现新行?
Go版本是go1.12.7 darwin/amd64。我有解决这个问题的方法,我只是从字符串中删除这个字符。
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)
type SitemapIndex struct {
Locations []string `xml:"sitemap>loc"`
}
type NewsMap struct {
Keyword string
Location string
}
type News struct {
Titles []string `xml:"url>news>title"`
Keywords []string `xml:"url>news>keywords"`
Locations []string `xml:"url>loc"`
}
func main() {
var s SitemapIndex
var n News
newsMap := make(map[string]NewsMap)
resp, _ := http.Get("https://washingtonpost.com/news-sitemaps/index.xml")
bytes, _ := ioutil.ReadAll(resp.Body)
xml.Unmarshal(bytes, &s)
for _, Location := range s.Locations {
tempURL := strings.Replace(Location, "n", "", -1) // how to avoid new lines character in url?
resp, err := http.Get(tempURL)
// do some stuff...
}
如果位置上没有此替换方法,我会收到错误 parse
https://www.washingtonpost.com/news-sitemaps/politics.xml
: net/url: invalid control character in URL
exit status 1
以下是示例 XML 文件https://www.washingtonpost.com/news-sitemaps/politics.xml
动漫人物
相关分类