Golang 實踐之 Discuz 論壇類比簽到

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

引言

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.