Golang 实践之 Discuz 论坛模拟签到

hldh214 · · 2030 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

引言

初学 Go 语言, 做做小玩意挽尊.
用到了 code.google.com/archive/p/mahonia/
贴代码:

代码

package main

import (
    "fmt"
    "io/ioutil"
    "mahonia"
    "net/http"
    "regexp"
    "strings"
)

func main() {
    // 用户 Cookie
    var cookie string = "oMVX_2132_saltkey=BQ1dWrGn;oMVX_2132_auth=95f6c%2BOg2r7RFCtaANsi%2FDKG8KHdIZeQ1PEfL79b8NVnVBZiyo%2F8RaRFCDqvO8qC2o34ND%2FpV1gDVYiNNOujs%2FZy048;"
    // regexp.MustCompile 返回一个值, 可用于常量定义
    // regexp.Compile 返回两个值, 第二个值表示错误
    hash_pattern := regexp.MustCompile(`formhash=(.+)">.+?</a>`)
    resp_pattern := regexp.MustCompile(`<div class="c">\s*?(.+?)<a href="`)

    client := &http.Client{}

    request, _ := http.NewRequest("GET", "http://bbs.fishc.com/plugin.php?id=dsu_paulsign:sign", nil)

    request.Header.Set(
        "Cookie",
        cookie)

    res, _ := client.Do(request)

    body_, _ := ioutil.ReadAll(res.Body)
    // fmt.Println(string(body_))

    defer res.Body.Close()

    formhash := hash_pattern.FindStringSubmatch(string(body_))[1]
    // fmt.Println(formhash)

    req, _ := http.NewRequest(
        "POST",
        "http://bbs.fishc.com/plugin.php?id=dsu_paulsign:sign&operation=qiandao&inajax=1",
        strings.NewReader("qdxq=kx&qdmode=2&todaysay=&fastreply=1&formhash="+formhash))

    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    req.Header.Set("Cookie", cookie)

    response, _ := client.Do(req)

    body, _ := ioutil.ReadAll(response.Body)

    defer response.Body.Close()

    output := mahonia.NewDecoder("GBK").ConvertString(string(body))

    result := resp_pattern.FindStringSubmatch(output)[1]

    fmt.Println(result)
}

小结

写起来像 Python
性能像 C


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

本文来自:CSDN博客

感谢作者:hldh214

查看原文:Golang 实践之 Discuz 论坛模拟签到

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

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