go爬虫 简单请求

 

demo1.go

 

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func fech(url string) string {
    client := &http.Client{}
    req, _ := http.NewRequest("GET", url, nil)
    req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36")
    req.Header.Set("cookie", "_ga_3Q0DVSGN10=GS1.1.1686653928.1.1.1686653937.51.0.0; _pbjs_userid_consent_data=3524755945110770; cto_bundle=mZmLSF85dUVNdXdwREVkeFE3b2VUajZ0S2klMkI5d3FreVlFenpJeFEyZ0w4RlpmTVZiaWt6Q2I1TGJkbXE3MXlTQlhPYXJLZEl4eURiT3Jjb3I3T0Vvd2xGdm1WQSUyQmNyeDhIUHlBSEpCM3hMUFVUMml3d2RhWmNXZzlzdiUyQkFXTFZIY1FnMg; cto_bidid=CdhCdl9BTlpmSjhpMzlUdTd2d25vSUZqdUNEczlGQ3J1S2h0ckdKU2drU2N1RTJzaTBzUnV0V1FwTXhUbXM3RFJKMUtVN0NGTzgwT2JwUXBoTHpUcmlNZ2RFZyUzRCUzRA; _ga_C2LFP3RFGH=GS1.1.1687754267.6.1.1687754335.0.0.0; _ga=GA1.2.1648403957.1686653923; Hm_lvt_866c9be12d4a814454792b1fd0fed295=1687143481,1687314851,1687751312,1688099485; .Cnblogs.AspNetCore.Cookies=CfDJ8DuWIYDefEVJtUW7VadsHY_tNh7ao1tXgttuUApjlm5XA7WC5kQXnelD_VS1z17zAe8z8QasViHjo-AIK79_CYAUkqcK7j_6gPGy0uxI4nY7Hw9ATFn7lUmc_Las-dx6rp_Wau0QyuwW6-W0ZtP0AFITAtTb2rh5Hr4LbpMll44MeY3yj4M4ffB2OtGFrNkpz4mIO1_7jngK-iNhKF3Sue74eLG-zLzqmjdxnGyxchyzgvCyhY1bG-i0EOAJuuawZXBcjIBMYEdlNBBKF9ztj5qwLlSVJdoAU5gvruowUcBGHr7uhsOHjYI4aG9jZCMjQE2d-kc2wPDXzqybIhIbRGDe_bwQ1Ge1KLq-pmWufRa5e-trq4IGwoOqoRsFm_Al8oEAhVjd_qlVDwKuS-W72Kb8iy--0BmP4LEHVHxvgvB-VslQ1FAl1luI7SM5EULkoC0c3gjsA-yvP0hwpzjrB6DnnDw4MPF-7oztMVy87YeIDKcGM08w8EUK7XMxLdwnYneSCidFxe-AGmoLpQ6ADwHvAg8cDpilu7tlPEhnz-wCbfhopH2QJtH3KTsRTysxY7UNoPR4EA06gMGSE06tbryxGYGNas29fdpjCLUmIu7L; .AspNetCore.Antiforgery.b8-pDmTq1XM=CfDJ8DuWIYDefEVJtUW7VadsHY9GzlkkyGKRFrnaEtkzRl8yCk-MRYDEAm84RV4g-GOp1poZxh_7ku6UhQJ_CdEowtSPN7Z7UkkKvDP-_v_fpBL4vEhFk8OoQuvzToqnIa2TLK5fFRAyMzZdCit2WIU9fTA; _gid=GA1.2.1595448807.1688099486; __gads=ID=6b54244ffc1be3d3:T=1686653929:RT=1688099487:S=ALNI_MbkkoHnP5-VRm-TUcciquZtVsaGCw; __gpi=UID=00000c12b511e32e:T=1686653929:RT=1688099487:S=ALNI_MbURVj9g0e9qRcg2YiA_kHj_7ha3A; Hm_lpvt_866c9be12d4a814454792b1fd0fed295=1688099495; lotame_domain_check=cnblogs.com; _gat_gtag_UA_476124_1=1")

    resp, err := client.Do(req)
    if err != nil {
        fmt.Println(err)
        return ""
    }
    if resp.StatusCode != 200 {
        fmt.Println("error")
        return ""
    }
    defer resp.Body.Close()

    //resp.Body
    var body []byte

    body, err = ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println(err)
        return ""
    }
    return string(body)
}

func main() {
    url := "https://www.cnblogs.com/angdh/"
    fmt.Println(fech(url))
}

 

posted @ 2023-06-30 12:48  AngDH  阅读(33)  评论(0编辑  收藏  举报