一个脚本签到功能

liuzhang · 2018-11-29 11:00:29 · 1795 次点击 · 预计阅读时间 4 分钟 · 大约8小时之前 开始浏览    
这是一个创建于 2018-11-29 11:00:29 的文章,其中的信息可能已经有所发展或是发生改变。

本人是 PHPer, 最近在学习GO, 用过 Yii 框架,应该熟悉 yiichina.com, 里面有个自动签到功能,用Go 练习写了一个自动签到功能

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
}

有疑问加站长微信联系(非本文作者))

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1795 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传