Go語言 爬蟲1-網絡請求


下面是找的幾個例子:

 

例子1:獲得百度首頁的html源文件: 

 
 
 
         package main
 
import(
    "fmt"
    "io/ioutil"
    "net/http"
)
 
func main(){
    response,_:=http.Get("http://www.baidu.com")
    defer response.Body.Close()
    body,_:=ioutil.ReadAll(response.Body)
    fmt.Println(string(body))
}

例子2,增加了一些錯誤驗證

代碼來自:https://gist.github.com/ijt/950790

 
 
 
         package main
 
import(
    "fmt"
    "io/ioutil"
    "net/http"
    "os"
)
 
func main(){
    response,err:=http.Get("http://www.baidu.com/")
    if err!=nil{
        fmt.Printf("%s",err)
        os.Exit(1)
    }else{
        defer response.Body.Close()
        contents,err:=ioutil.ReadAll(response.Body)
        if err!=nil{
            fmt.Printf("%s",err)
            os.Exit(1)
        }
        fmt.Printf("%s\n",string(contents))
    }
}

http下有Get,Post,PostForm三個函數。這三個函數直接實現了簡單的http客戶端

下一個簡單的例子增加了log, http://gameor.com/archives/178/golang%E5%8F%96%E9%93%BE%E6%8E%A5%E4%B8%8Ephp%E6%AF%94%E8%BE%83%E4%B8%8B/

 
 
 
         package main
 
import(
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
)
 
func main(){
    res,err:=http.Get("http://www.ghj1976.net/")
    if err!=nil{
        log.Fatal(err)
    }
    defer res.Body.Close()
    robots,err:=ioutil.ReadAll(res.Body)
    if err!=nil{
        log.Fatal(err)
    }
    fmt.Printf("%s",robots)
}

例子3:把百度的網頁存在本地一個文件:

http://david-je.iteye.com/blog/1602774

 
 
 
         package main
 
import(
    "fmt"
    "log"
    "net/http"
    "os"
)
 
func main(){
    resp,err:=http.Get("http://www.baidu.com")
    if err!=nil{
        //handleerror
        fmt.Println(err)
        log.Fatal(err)
    }
    defer resp.Body.Close()
    if resp.StatusCode==http.StatusOK{
        fmt.Println(resp.StatusCode)
    }
 
    buf:=make([]byte,1024)
    //createfile
    f,err1:=os.OpenFile("baidu.html",os.O_RDWR|os.O_CREATE|os.O_APPEND,os.ModePerm)
    if err1!=nil{
        panic(err1)
        return
    }
    defer f.Close()
 
    for{
        n,_:=resp.Body.Read(buf)
        if 0==n{
            break
        }
        f.WriteString(string(buf[:n]))
    }
 
}
 
 
其他可以借鑒的
 
golang 批量檢查頁面
http://www.simonzhang.net/?p=1346

 

除了使用Get、Post、PostForm 這三個函數來建立一個簡單客戶端,還可以使用:
http.Client和http.NewRequest來模擬請求

例子:指定公共頭的請求百度頁面

http://www.cnblogs.com/yjf512/archive/2012/06/18/2554066.html

 
 
 
         package main
 
import(
    "fmt"
    "io/ioutil"
    "net/http"
)
 
func main(){
    client:=&http.Client{}
    reqest,_:=http.NewRequest("GET","http://www.baidu.com",nil)
 
    reqest.Header.Set("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
    reqest.Header.Set("Accept-Charset","GBK,utf-8;q=0.7,*;q=0.3")
    reqest.Header.Set("Accept-Encoding","gzip,deflate,sdch")
    reqest.Header.Set("Accept-Language","zh-CN,zh;q=0.8")
    reqest.Header.Set("Cache-Control","max-age=0")
    reqest.Header.Set("Connection","keep-alive")
 
    response,_:=client.Do(reqest)
    if response.StatusCode==200{
        body,_:=ioutil.ReadAll(response.Body)
        bodystr:=string(body)
        fmt.Println(bodystr)
    }
}

 

 

參考資料:

用golang的正則regexp:去除HTML,CSS,SCRIPT代碼,僅保留頁面文字
http://bpbp.iteye.com/blog/1668869

 

Golang解析網頁入門
http://mjason.github.com/blog/2013/01/29/golangjie-xi-wang-ye-ru-men/

golang做的webCrawl: gocrawl
http://hi.baidu.com/izouying/item/208551ec90a726f32a09a457

一個Go語言實現的web爬蟲
http://www.sharejs.com/codes/go/4416


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM