Esempio n. 1
0
func detectCharset(source []byte) (charset string) {
	if doc, err := goquery.NewDocumentFromReader(bytes.NewReader(source)); err == nil {
		if ss := doc.Find("meta[charset]"); ss.Length() > 0 {
			charset, _ = ss.Attr("charset")
			charset = strings.ToLower(charset)
			return
		}

		ss := doc.Find("meta[http-equiv]")
		for i, l := 0, ss.Length(); i < l; i++ {
			s := ss.Eq(i)
			if h, _ := s.Attr("http-equiv"); strings.ToLower(h) == "content-type" {
				contentType, _ := s.Attr("content")
				charset, _, _ = CharsetFromContentType(contentType)
				return
			}
		}
	} else {
		log(err)
	}
	return
}
Esempio n. 2
0
// Get goquery document.
func GetDocument(client *http.Client, link, cookie string, form url.Values, prepare func(request *http.Request)) (document *goquery.Document, response *http.Response, err error) {
	if client == nil {
		client = DefaultClient
	}

	response, err = Request(client, link, cookie, form, prepare)
	if err != nil {
		return
	}
	defer response.Body.Close()

	stream, _, err := TextStreamFromResponse(response, func(textType string) (err error) {
		if textType != "html" {
			err = errors.New("The type of response is [" + textType + "] but not html.")
		}
		return
	})
	if err != nil {
		return
	}

	document, err = goquery.NewDocumentFromReader(stream)
	return
}