例如我用http包弄了个io.reader 怎么检测里面的编码是啥
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
func find_charse_list(Typelist []string) string { //查找文件编码形式 for , value := range Type_list { //fmt.Printf("arr[%s]=%s \n", index, value) if strings.Contains(value, " ") { //去除首尾空格 value = strings.Trim(value, " ") //头部 value = strings.TrimSpace(value) //尾部 } if strings.Contains(value, "charset=") { return strings.Replace(value, "charset=", "", -1) } } return "utf-8" }
func main() { _, Server_Content_Type, content, rand_bool := http_get_post.Get_url_datax("http://www.ygdy8.com/" Server_charset := "" if rand_bool { Type_list := strings.Split(Server_Content_Type, ";") if len(Server_Content_Type) >= 2 { Server_Content_Type = string(Type_list[0]) } fmt.Printf("==%v==%v==\n", Server_Content_Type, len(content)) if strings.Contains(Server_Content_Type, "text/") { //"text/html" if strings.Contains(Server_Content_Type, "charset=") { if len(Type_list) >= 1 { Server_charset = find_charse_list(Typelist) //查找文件编码形式 } } else { //在网页里找编码格式 docxx, := goquery.NewDocumentFromReader(strings.NewReader(content)) docxx.Find("[content]").Each(func(i int, s *goquery.Selection) { contentxx, err := s.Attr("content") if len(contentxx) == 0 || !err { } else { if strings.Contains(contentxx, "charset=") { Type_listx := strings.Split(contentxx, ";") if len(Type_list) >= 1 { Server_charset = find_charse_list(Type_listx) //查找文件编码形式 } } } }) } fmt.Printf("==%v==\n", Server_charset) } }
}
这是我网页检测
`单行代码`
func find_charse_list(Typelist []string) string { //查找文件编码形式 for , value := range Type_list { //fmt.Printf("arr[%s]=%s \n", index, value) if strings.Contains(value, " ") { //去除首尾空格 value = strings.Trim(value, " ") //头部 value = strings.TrimSpace(value) //尾部 } if strings.Contains(value, "charset=") { return strings.Replace(value, "charset=", "", -1) } } return "utf-8" }
func main() { _, Server_Content_Type, content, rand_bool := http_get_post.Get_url_datax("http://www.ygdy8.com/"
Server_charset := ""
if rand_bool {
Type_list := strings.Split(Server_Content_Type, ";")
if len(Server_Content_Type) >= 2 {
Server_Content_Type = string(Type_list[0])
}
fmt.Printf("==%v==%v==\n", Server_Content_Type, len(content))
if strings.Contains(Server_Content_Type, "text/") { //"text/html"
if strings.Contains(Server_Content_Type, "charset=") {
if len(Type_list) >= 1 {
Server_charset = find_charse_list(Typelist) //查找文件编码形式
}
} else { //在网页里找编码格式
docxx, := goquery.NewDocumentFromReader(strings.NewReader(content))
docxx.Find("[content]").Each(func(i int, s *goquery.Selection) {
contentxx, err := s.Attr("content")
if len(contentxx) == 0 || !err {
} else {
if strings.Contains(contentxx, "charset=") {
Type_listx := strings.Split(contentxx, ";")
if len(Type_list) >= 1 {
Server_charset = find_charse_list(Type_listx) //查找文件编码形式
}
}
}
})
}
fmt.Printf("==%v==\n", Server_charset)
}
}
}
这是我网页检测