- A+
所属分类:编程茶楼
package main import ( "fmt" "io/ioutil" "net/http" "regexp" "strings" ) func main() { // 模拟登录获取cookie和token cookie, token := login() // 获取文章列表 articleLinks := getArticleList(cookie, token) // 遍历文章链接获取文章内容 for _, link := range articleLinks { content := getArticleContent(cookie, token, link) // 处理文章内容 fmt.Println(content) } } // 模拟登录 func login() (string, string) { // TODO: 模拟登录,获取cookie和token } // 获取文章列表 func getArticleList(cookie, token string) []string { url := "https://mp.weixin.qq.com/mp/profile_ext?action=getmsg&__biz=BIZ_ID&f=json&offset=0&count=10&is_ok=1&scene=&uin=&key=&pass_ticket=&wxtoken=&appmsg_token=APPMSG_TOKEN&x5=0&f=json" url = strings.ReplaceAll(url, "BIZ_ID", "微信公众号的biz字段") url = strings.ReplaceAll(url, "APPMSG_TOKEN", token) req, err := http.NewRequest("GET", url, nil) if err != nil { panic(err) } req.Header.Set("Cookie", cookie) req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36") client := &http.Client{} resp, err := client.Do(req) if err != nil { panic(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { panic(err) } re := regexp.MustCompile(`"http.*?"`) matches := re.FindAllString(string(body), -1) var articleLinks []string for _, match := range matches { link := strings.ReplaceAll(match, """, "") articleLinks = append(articleLinks, link) } return articleLinks } // 获取文章内容 func getArticleContent(cookie, token, link string) string { req, err := http.NewRequest("GET", link, nil) if err != nil { panic(err) } req.Header.Set("Cookie", cookie) req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,
- 我的微信
- 这是我的微信扫一扫
- 我的微信公众号
- 我的微信公众号扫一扫