Golang 实践之 Discuz 论坛模拟签到

引言

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

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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