Skip to content

Instantly share code, notes, and snippets.

@kkdai
Created January 27, 2020 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkdai/77d2061d0221d5e9996d412c1a5e5b7b to your computer and use it in GitHub Desktop.
Save kkdai/77d2061d0221d5e9996d412c1a5e5b7b to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
)
func parseWebUrl(url string) {
resp := getResponseWithCookie(target)
doc, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
log.Fatal(err)
}
// 找出所有 class id 名稱為 `r-ent`
doc.Find(".r-ent").Each(func(i int, s *goquery.Selection) {
//這樣就能找到資料了 .....
////....
})
}
func getResponseWithCookie(url string) *http.Response {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal("http failed:", err)
}
req.AddCookie(&http.Cookie{Name: "over18", Value: "1"})
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal("client failed:", err)
}
return resp
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment