func ConvertToString(src string, srcCode string, tagCode string) string { srcCoder := mahonia.NewDecoder(srcCode) srcResult := srcCoder.ConvertString(src) tagCoder := mahonia.NewDecoder(tagCode) _, cdata, _ := tagCoder.Translate([]byte(srcResult), true) result := string(cdata) return result }
func getDocument(url string) *goquery.Document { req, err := http.NewRequest("GET", url, nil) if err == nil { req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8") // req.Header.Add("Accept-Encoding", "gzip, deflate, sdch") //有这个会乱码 req.Header.Add("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6") req.Header.Add("Cache-Control", "max-age=0") req.Header.Add("Connection", "keep-alive") req.Header.Add("Host", "www.rong360.com") req.Header.Add("Upgrade-Insecure-Requests", "1") req.Header.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2490.71 Safari/537.36") resp, err := client.Do(req) if err == nil && resp.Body != nil { decoder := mahonia.NewDecoder("UTF-8") document, err := goquery.NewDocumentFromReader(decoder.NewReader(resp.Body)) if err == nil { return document } } } if err != nil { beego.Error(err) } return nil }
func main() { flag.Parse() var r io.Reader = os.Stdin var w io.Writer = os.Stdout if *from != "utf-8" { decode := mahonia.NewDecoder(*from) if decode == nil { log.Fatalf("Could not create decoder for %s", *from) } r = decode.NewReader(r) } if *to != "utf-8" { encode := mahonia.NewEncoder(*to) if encode == nil { log.Fatalf("Could not create decoder for %s", *to) } w = encode.NewWriter(w) } io.Copy(w, r) }
func DecodeString(src, charset string) string { return mahonia.NewDecoder(charset).ConvertString(src) }