func (this *HttpDownloader) downloadHtml(p *page.Page, req *context.Request) *page.Page { var err error p, destbody := this.downloadFile(p, req) //fmt.Printf("Destbody %v \r\n", destbody) if !p.IsSucc() { //fmt.Print("Page error \r\n") return p } bodyReader := bytes.NewReader([]byte(destbody)) var doc *goquery.Document if doc, err = goquery.NewDocumentFromReader(bodyReader); err != nil { log.Error(err.Error()) p.SetStatus(true, err.Error()) return p } var body string if body, err = doc.Html(); err != nil { log.Error(err.Error()) p.SetStatus(true, err.Error()) return p } p.SetBodyStr(body).SetHtmlParser(doc).SetStatus(false, "") return p }
func (this *HttpDownloader) changeCharsetEncodingAutoGzipSupport(contentTypeStr string, sor io.ReadCloser) string { var err error gzipReader, err := gzip.NewReader(sor) if err != nil { log.Error(err.Error()) return "" } defer gzipReader.Close() destReader, err := charset.NewReader(gzipReader, contentTypeStr) if err != nil { log.Error(err.Error()) destReader = sor } var sorbody []byte if sorbody, err = ioutil.ReadAll(destReader); err != nil { log.Error(err.Error()) // For gb2312, an error will be returned. // Error like: simplifiedchinese: invalid GBK encoding // return "" } //e,name,certain := charset.DetermineEncoding(sorbody,contentTypeStr) bodystr := string(sorbody) return bodystr }
// choose http GET/method to download func connectByHttp(p *page.Page, req *context.Request) (*http.Response, error) { client := &http.Client{ CheckRedirect: req.GetRedirectFunc(), } httpreq, err := http.NewRequest(req.GetMethod(), req.GetUrl(), strings.NewReader(req.GetPostdata())) if header := req.GetHeader(); header != nil { httpreq.Header = req.GetHeader() } if cookies := req.GetCookies(); cookies != nil { for i := range cookies { httpreq.AddCookie(cookies[i]) } } var resp *http.Response if resp, err = client.Do(httpreq); err != nil { if e, ok := err.(*url.Error); ok && e.Err != nil && e.Err.Error() == "normal" { // normal } else { log.Error(err.Error()) p.SetStatus(true, err.Error()) //fmt.Printf("client do error %v \r\n", err) return nil, err } } return resp, nil }
func (this *HttpDownloader) downloadJson(p *page.Page, req *context.Request) *page.Page { var err error p, destbody := this.downloadFile(p, req) if !p.IsSucc() { return p } var body []byte body = []byte(destbody) mtype := req.GetResponceType() if mtype == "jsonp" { tmpstr := utils.JsonpToJson(destbody) body = []byte(tmpstr) } var r *simplejson.Json if r, err = simplejson.NewJson(body); err != nil { log.Error(string(body) + "\t" + err.Error()) p.SetStatus(true, err.Error()) return p } // json result p.SetBodyStr(string(body)).SetJson(r).SetStatus(false, "") return p }
// GetHtmlParser returns goquery object binded to target crawl result. func (this *Page) ResetHtmlParser() *goquery.Document { r := strings.NewReader(this.body) var err error this.docParser, err = goquery.NewDocumentFromReader(r) if err != nil { log.Error(err.Error()) panic(err.Error()) } return this.docParser }
func (this *HttpDownloader) Download(req *context.Request) *page.Page { var mtype string var p = page.NewPage(req) mtype = req.GetResponceType() switch mtype { case "html": return this.downloadHtml(p, req) case "json": fallthrough case "jsonp": return this.downloadJson(p, req) case "text": return this.downloadText(p, req) default: log.Error("error request type:" + mtype) } return p }
func readHeaderFromFile(headerFile string) http.Header { //read file , parse the header and cookies b, err := ioutil.ReadFile(headerFile) if err != nil { //make be: share access error log.Error(err.Error()) return nil } js, _ := simplejson.NewJson(b) //constructed to header h := make(http.Header) h.Add("User-Agent", js.Get("User-Agent").MustString()) h.Add("Referer", js.Get("Referer").MustString()) h.Add("Cookie", js.Get("Cookie").MustString()) h.Add("Cache-Control", "max-age=0") h.Add("Connection", "keep-alive") return h }
// Download file and change the charset of page charset. func (this *HttpDownloader) downloadFile(p *page.Page, req *context.Request) (*page.Page, string) { var err error var urlstr string if urlstr = req.GetUrl(); len(urlstr) == 0 { log.Error("url is empty") p.SetStatus(true, "url is empty") return p, "" } var resp *http.Response if proxystr := req.GetProxyHost(); len(proxystr) != 0 { //using http proxy //fmt.Print("HttpProxy Enter ",proxystr,"\n") resp, err = connectByHttpProxy(p, req) } else { //normal http download //fmt.Print("Http Normal Enter \n",proxystr,"\n") resp, err = connectByHttp(p, req) } if err != nil { return p, "" } //b, _ := ioutil.ReadAll(resp.Body) //fmt.Printf("Resp body %v \r\n", string(b)) p.SetHeader(resp.Header) p.SetCookies(resp.Cookies()) // get converter to utf-8 var bodyStr string if resp.Header.Get("Content-Encoding") == "gzip" { bodyStr = this.changeCharsetEncodingAutoGzipSupport(resp.Header.Get("Content-Type"), resp.Body) } else { bodyStr = this.changeCharsetEncodingAuto(resp.Header.Get("Content-Type"), resp.Body) } //fmt.Printf("utf-8 body %v \r\n", bodyStr) defer resp.Body.Close() return p, bodyStr }