一个脚本签到功能

栏目: 编程工具 · 发布时间: 5年前

本人是 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>


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

大师之路

大师之路

赵鹏 / 人民邮电出版社 / 2007-12 / 79.00元

《大师之路:Photoshop中文版完全解析》以介绍Photoshop选区、图层、路径3大基础为主线,从色彩原理入手,注重基础知识,辅以大量实例,全面剖析了Photoshop的应用。对于其他内容,如色彩调整和各类工具的应用,也均采用与实际操作相结合的方式进行讲解。另外,还简单明了地介绍了扩展应用的领域。所附教学光盘的内容可自成一套独立的视频教学体系。本书完全按照初学者的认知角度编写,重点介绍图像原......一起来看看 《大师之路》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具