Golang 实践之 Discuz 论坛模拟签到

引言

初学 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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值