[Golang] 纯文本查看 复制代码 package main
import (
"fmt"
"net/http"
"net/url"
"strings"
"github.com/PuerkitoBio/goquery"
)
func main() {
// 设置登录的账号和密码
username := "your_username"
password := "your_password"
// 建立HTTP客户端并发送登录请求
client := &http.Client{}
loginURL := "https://login.taobao.com/member/login.jhtml"
req, err := http.NewRequest("GET", loginURL, nil)
if err != nil {
fmt.Println("Failed to create login request")
return
}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Failed to send login request")
return
}
// 获取登录所需的参数
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
fmt.Println("Failed to parse login page")
return
}
loginForm := doc.Find("#J_LoginForm")
action, _ := loginForm.Attr("action")
loginParams := url.Values{}
loginForm.Find("input").Each(func(_ int, s *goquery.Selection) {
name, _ := s.Attr("name")
value, _ := s.Attr("value")
loginParams.Set(name, value)
})
// 将账号和密码添加到登录参数中
loginParams.Set("TPL_username", username)
loginParams.Set("TPL_password", password)
// 发送登录POST请求
loginReq, err := http.NewRequest("POST", action, strings.NewReader(loginParams.Encode()))
if err != nil {
fmt.Println("Failed to create login POST request")
return
}
loginReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
loginResp, err := client.Do(loginReq)
if err != nil {
fmt.Println("Failed to send login POST request")
return
}
// 打印登录后的页面内容
loggedInDoc, err := goquery.NewDocumentFromReader(loginResp.Body)
if err != nil {
fmt.Println("Failed to parse logged in page")
return
}
fmt.Println(loggedInDoc.Text())
} |