這是一個建立於
的文章,其中的資訊可能已經有所發展或是發生改變。
引言
初學 Go 語言, 做做小玩意挽尊.
用到了 code.google.com/archive/p/mahonia/ 包
貼代碼:
代碼
package mainimport ( "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