第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

golang HTML 字符集解碼

golang HTML 字符集解碼

Go
拉丁的傳說 2022-01-04 10:51:36
我正在嘗試解碼非utf-8 編碼的HTML 頁面。<meta http-equiv="Content-Type" content="text/html; charset=gb2312">有沒有可以做到這一點(diǎn)的圖書館?我在網(wǎng)上找不到一個(gè)。PS 當(dāng)然,我可以使用 goquery 和 iconv-go 提取字符集并解碼 HTML 頁面,但我不想重新發(fā)明輪子。
查看完整描述

2 回答

?
揚(yáng)帆大魚

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個(gè)贊

Golang 官方提供了擴(kuò)展包:charset和encoding。


下面的代碼確保 HTML 包可以正確解析文檔:


func detectContentCharset(body io.Reader) string {

    r := bufio.NewReader(body)

    if data, err := r.Peek(1024); err == nil {

        if _, name, ok := charset.DetermineEncoding(data, ""); ok {

            return name

        }

    }

    return "utf-8"

}


// Decode parses the HTML body on the specified encoding and

// returns the HTML Document.

func Decode(body io.Reader, charset string) (interface{}, error) {

    if charset == "" {

        charset = detectContentCharset(body)

    }

    e, err := htmlindex.Get(charset)

    if err != nil {

        return nil, err

    }


    if name, _ := htmlindex.Name(e); name != "utf-8" {

        body = e.NewDecoder().Reader(body)

    }


    node, err := html.Parse(body)

    if err != nil {

        return nil, err

    }

    return node, nil

}


查看完整回答
反對(duì) 回復(fù) 2022-01-04
?
交互式愛情

TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊

goquery可以滿足您的需求。例如:


import "https://github.com/PuerkitoBio/goquery"


func main() {

    d, err := goquery.NewDocument("http://www.google.com")

    dh := d.Find("head")

    dc := dh.Find("meta[http-equiv]")

    c, err := dc.Attr("content") // get charset

    // ...

}

更多的操作可以在Document結(jié)構(gòu)中找到。


查看完整回答
反對(duì) 回復(fù) 2022-01-04
  • 2 回答
  • 0 關(guān)注
  • 234 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)