本人是 PHPer, 最近在学习GO, 用过 Yii 框架,应该熟悉 yiichina.com, 里面有个自动签到功能,用 Go 练习写了一个自动签到功能 <pre> package main import ( "io/ioutil" "net/http" "net/http/cookiejar" "net/url" "os" "regexp" "strings" "time" ) var cookies []*http.Cookie const ( login_url = "https://www.yiichina.com/login" sign_url = "https://www.yiichina.com/registration" ) func main() { /******************** 登录开始 ****************************/ c := &http.Client{} csrf := getCsrf(c, login_url, true) var postData = url.Values{} postData.Add("_csrf", csrf) postData.Add("LoginForm[username]", "*******") postData.Add("LoginForm[password]", "*******") postData.Add("LoginForm[rememberMe]", "0") postData.Add("LoginForm[rememberMe]", "1") postData.Add("login-button", "") postDataStr := postData.Encode() loginReq, _ := http.NewRequest("POST", login_url, strings.NewReader(postDataStr)) loginReq.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8") loginReq.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8") loginReq.Header.Set("Cache-Control", "no-cache") loginReq.Header.Set("Connection","keep-alive") loginReq.Header.Set("Content-Length","250") loginReq.Header.Set("Content-Type","application/x-www-form-urlencoded") loginReq.Header.Set("Host","www.yiichina.com") loginReq.Header.Set("Origin", "https://www.yiichina.com") loginReq.Header.Set("Pragma", "no-cache") loginReq.Header.Set("Referer", "https://www.yiichina.com/login") loginReq.Header.Set("Upgrade-Insecure-Requests", "1") loginReq.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36") for _, v := range cookies { loginReq.AddCookie(v) } jar, _ := cookiejar.New(nil) c.Jar = jar resp, _ := c.Do(loginReq) cookies = resp.Cookies() /******************** 签到开始 ****************************/ _csrf := getCsrf(c, sign_url, false) var signPostData = url.Values{} signPostData.Add("_csrf", _csrf) signPostStr := signPostData.Encode() signReq, _ := http.NewRequest("POST", sign_url, strings.NewReader(signPostStr)) signReq.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8") signReq.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8") signReq.Header.Set("Cache-Control", "no-cache") signReq.Header.Set("Connection","keep-alive") signReq.Header.Set("Content-Length","250") signReq.Header.Set("Content-Type","application/x-www-form-urlencoded") signReq.Header.Set("Host","www.yiichina.com") signReq.Header.Set("Origin", "https://www.yiichina.com") signReq.Header.Set("Pragma", "no-cache") signReq.Header.Set("Referer", "https://www.yiichina.com/") signReq.Header.Set("X-Requested-With", "XMLHttpRequest") signReq.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36") for _, v := range cookies { signReq.AddCookie(v) } res, _ := c.Do(signReq) content, _ := ioutil.ReadAll(res.Body) name := "E:/go/1.txt" str_time := time.Now().Format("2006-01-02 15:04:05") f, _ := os.OpenFile(name, os.O_WRONLY|os.O_APPEND, 0666) logStr := str_time + " : " + string(content) + "\n" f.Write([]byte(logStr)) } func getCsrf(c * http.Client, url string, isSetCookie bool) string { req, _ := http.NewRequest("GET", url, nil) res, _ := c.Do(req) data, _ := ioutil.ReadAll(res.Body) re, _ := regexp.Compile("<meta name=\"csrf-token\" content=\"(.+)\">") submatch := re.FindSubmatch([]byte(data)) if isSetCookie { cookies = res.Cookies() } csrf := string(submatch[1]) return csrf } </pre>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- iOS 百度地图定位签到实现
- [python实例] 爬虫实现自动登录、签到
- 从LCTF WEB签到题看PHP反序列化
- b 站账号快速升级到 Lv6:每天自动签到,观看,分享,投币视频
- 脚本文件里的 Hybrid Script(混合式脚本)
- 脚本错误量极致优化-定位压缩且无 SourceMap 文件的脚本错误
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Powerful
Patty McCord / Missionday / 2018-1-25
Named by The Washington Post as one of the 11 Leadership Books to Read in 2018 When it comes to recruiting, motivating, and creating great teams, Patty McCord says most companies have it all wrong. Mc......一起来看看 《Powerful》 这本书的介绍吧!