我想获得我的leetcode排名,但我对html和JavaScript了解一点点。经过多次尝试,我得到了这个输出。
aQuaYi的排名是排名:{[{ 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")
}
请帮我完成这段代码,非常感谢。
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]
}