简单的Go语言爬虫模拟登陆,Cookie

包含了模拟登陆,Cookie存储,获取对应页面

具体函数的含义可以从网上去搜索

package main
import (
	"fmt"
	"golang.org/x/net/publicsuffix"
	"io/ioutil"
	"net/http/cookiejar"
	"net/http"
	"net/url"
)
loginUrl := "XXXXXx"
		v := url.Values{
		"username":[]string{"XXXXXx"},
		"password":[]string{"XXXXXx"},
	}

	options := cookiejar.Options{
		PublicSuffixList: publicsuffix.List,
	}
	jar, _ := cookiejar.New(&options)
	client := &http.Client{
		Jar: jar,
	}
	resp, _ := client.PostForm(loginUrl,v)
	body, _ := ioutil.ReadAll(resp.Body)  // 读取Body
	fmt.Println(string(body))
	resp, _ = client.Get("XXXXXx")
	body, _ = ioutil.ReadAll(resp.Body)  // 读取Body
	fmt.Println(string(body))

你可能感兴趣的:(Go,golang,爬虫)