0

我想得到我的 leetcode 排名,但我对 html 和 JavaScript 了解一点。经过大量尝试,我得到了这个输出。

aQuaYi 的排名是 Ranking: {[{ pc.ranking }]}

来源是

package main
import (
    "fmt"
    "log"

    "github.com/PuerkitoBio/goquery"
)

func showRanking(username string) {
    URL := fmt.Sprintf("https://leetcode.com/%s", username)
    doc, err := goquery.NewDocument(URL)
    if err != nil {
        log.Fatal(err)
    }

    ranking, _ := doc.Find("div.panel-body").Find("span.ranking").Attr("data-content")

    fmt.Printf("%s's ranking is %v", username, ranking)
}

func main() {
    showRanking("aQuaYi")
}

请帮我完成这段代码,非常感谢。

4

1 回答 1

0
func getRanking(username string) string {
    URL := fmt.Sprintf("https://leetcode.com/%s/", username)
    fmt.Println(URL)

    data := getRaw(URL) // or your way to get raw html page down
    str := string(data)
    i := strings.Index(str, "ng-init")
    j := i + strings.Index(str[i:], "ng-cloak")
    str = str[i:j]

    i = strings.Index(str, "(")
    j = strings.Index(str, ")")
    str = str[i:j]

    strs := strings.Split(str, ",")
    ans := strs[5]
    i = strings.Index(ans, "'")
    j = 2 + strings.Index(ans[2:], "'")

    return ans[i+1 : j]
}
于 2017-08-18T13:49:03.790 回答