package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"io"
"log"
"regexp"
"strings"
)
var cookies_lagou []*http.Cookie
const (
login_url_lagou string = "https://passport.lagou.com/login/login.html"
post_login_info_url_lagou string = "https://passport.lagou.com/login/login.json"
username_lagou string = "13330295142"
password_lagou string = "4525674692ac06e619cdb3f1b4b65b08"
)
func getToken(contents io.Reader) (string,string){
data, _ := ioutil.ReadAll(contents)
regCode := regexp.MustCompile(`X_Anti_Forge_Code\s+\=(.+?);`)
if regCode == nil {
log.Fatal("解析Code出錯...")
}
//提取關(guān)鍵信息
code := regCode.FindAllStringSubmatch(string(data), -1)[0][1]
regToken := regexp.MustCompile(`X_Anti_Forge_Token\s+\=(.+?);`)
if regToken == nil {
fmt.Println("MustCompile err")
}
//提取關(guān)鍵信息
token := regToken.FindAllStringSubmatch(string(data), -1)[0][1]
return token,code
}
func login_lagou() {
//獲取登陸界面的cookie
jar, _ := cookiejar.New(nil)
client := &http.Client{
Jar: jar,
}
req, _ := http.NewRequest("GET", login_url_lagou, nil)
res, _ := client.Do(req)
for k, v := range res.Cookies() {
fmt.Printf("%v=%v\n",k,v)
}
token,code := getToken(res.Body)
//post數(shù)據(jù)
postValues := url.Values{}
postValues.Add("isValidate", "true")
postValues.Add("username", username_lagou)
postValues.Add("password", password_lagou)
postValues.Add("request_form_verifyCode", "")
postValues.Add("submit", "")
body := ioutil.NopCloser(strings.NewReader(postValues.Encode())) //把form數(shù)據(jù)編下碼
requ, _ := http.NewRequest("POST", post_login_info_url_lagou, body)
requ.Header.Set("X-Requested-With","XMLHttpRequest")
requ.Header.Set("Content-Type","application/x-www-form-urlencoded; charset=UTF-8")
requ.Header.Set("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36")
requ.Header.Set("Host","passport.lagou.com")
requ.Header.Set("Origin","https://passport.lagou.com")
requ.Header.Add("X-Anit-Forge-Token",token)
requ.Header.Add("X-Anit-Forge-Code",code)
requ.Header.Set("Accept","application/json, text/javascript, */*; q=0.01")
requ.Header.Set("Connection","keep-alive")
requ.Header.Set("Accept-Language","en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7")
requ.Header.Set("Accept-Encoding","gzip, deflate, br")
//requ.Header.Set("Content-Length","111")
//requ.Header.Set("Cookie","JSESSIONID=ABAAABAAAHAAAFD632D7028F2AC7466F2DAA44C1BE15A01; user_trace_token=20180418184827-7aec3c11-930a-494a-9a73-c83419b1450a; X_HTTP_TOKEN=7ab0dd4e12bf94d2d4c0c3370e1c6341; _ga=GA1.2.837634606.1524048588; _gid=GA1.2.1349141044.1524048588; _ga=GA1.3.837634606.1524048588; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1523518887,1524030389,1524033687,1524045972; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1524048588; LGSID=20180418184827-06c3d493-42f6-11e8-8a1c-525400f775ce; PRE_UTM=; PRE_HOST=; PRE_SITE=; PRE_LAND=http%3A%2F%2Fpassport.lagou.com%2Flogin%2Flogin.html; LGUID=20180418184827-06c3d687-42f6-11e8-8a1c-525400f775ce; LGRID=20180418185212-8ca212e6-42f6-11e8-b8a8-5254005c3644; TG-TRACK-CODE=undefined")
for _, v := range res.Cookies() {
requ.AddCookie(v)
}
res,_ = client.Do(requ)
//cookies_lagou = res.Cookies()
data, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
fmt.Println(string(data))
}
func main() {
login_lagou()
}
以上代碼運行結(jié)果為:
0=JSESSIONID=ABAAABAAAHAAAFD5DA9395672E82BF6CEDCAE3CA350ED64; Path=/; HttpOnly
1=user_trace_token=20180418221336-b03f8889-f175-46cf-b986-af55a8ef3e33; Path=/; Domain=lagou.com; Max-Age=31536000
{"content":{"rows":[]},"message":"非法請求,參數(shù)錯誤","state":299}
Process finished with exit code 0
我參考的是這篇文章python -- 拉勾網(wǎng)爬蟲模擬登錄 在python中,有requests包,requests.session中自動保留了前面的cookies,session信息, 但是用golang實現(xiàn)的時候就不行,也設(shè)置了cookies,把login.html的cookies遍歷添加了,再去請求登錄還是不行。 在實際的登錄中,cooike好像多了一些東西,不知道是不是這個的緣故。 瀏覽器登錄Header信息:
Accept: application/json, text/javascript, */*; q=0.01
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7
Connection: keep-alive
Content-Length: 111
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Cookie: user_trace_token=20180418184827-7aec3c11-930a-494a-9a73-c83419b1450a; _ga=GA1.2.837634606.1524048588; _gid=GA1.2.1349141044.1524048588; _ga=GA1.3.837634606.1524048588; LGUID=20180418184827-06c3d687-42f6-11e8-8a1c-525400f775ce; index_location_city=%E6%88%90%E9%83%BD; gate_login_token=e27e32a29e46e476b94f06e8e0a5b6d149dbb2a5efd97c51; LGSID=20180418212135-6b42e55b-430b-11e8-b8a9-5254005c3644; PRE_UTM=; PRE_HOST=; PRE_SITE=; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2F; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1524030389,1524033687,1524045972,1524057776; JSESSIONID=ABAAABAAAHAAAFD5346FCED99BA9CA2991F52D85C9D0982; X_HTTP_TOKEN=7ab0dd4e12bf94d2d4c0c3370e1c6341; TG-TRACK-CODE=undefined; _gat=1; LGRID=20180418213214-e7ac4b8b-430c-11e8-8a7e-525400f775ce; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1524058415
Host: passport.lagou.com
Origin: https://passport.lagou.com
Referer: https://passport.lagou.com/login/login.html
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36
X-Anit-Forge-Code: 21248807
X-Anit-Forge-Token: cc96fefa-0bad-4a06-8674-48fc1b42e125
X-Requested-With: XMLHttpRequest
請問各位前輩,是我的代碼有問題嗎?自己已經(jīng)百度,google了兩天,各種折騰,甚至把python代碼拉下來運行,但是可以模擬登陸,golang就不行,實在沒辦法,所以來論壇求助各位前輩,求前輩們指點,感激不盡。
https://segmentfault.com/q/10...
——————————————————————————————
第一次回復(fù)時沒看你的內(nèi)容,只回應(yīng)了標題所以貼了個網(wǎng)址
下面正式回復(fù)
你的問題不是cookies的問題,我根據(jù)那篇文章改了下運行成功了,你自己對照下
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"regexp"
"strings"
)
var cookies_lagou []*http.Cookie
const (
login_url_lagou string = "https://passport.lagou.com/login/login.html"
post_login_info_url_lagou string = "https://passport.lagou.com/login/login.json"
username_lagou string = "13330295142"
password_lagou string = "4525674692ac06e619cdb3f1b4b65b08"
)
func getToken(contents io.Reader) (string, string) {
data, _ := ioutil.ReadAll(contents)
regCode := regexp.MustCompile(`X_Anti_Forge_Code = '(.*?)';`)
if regCode == nil {
log.Fatal("解析Code出錯...")
}
//提取關(guān)鍵信息
code := regCode.FindAllStringSubmatch(string(data), -1)[0][1]
regToken := regexp.MustCompile(`X_Anti_Forge_Token = '(.*?)';`)
if regToken == nil {
fmt.Println("MustCompile err")
}
//提取關(guān)鍵信息
token := regToken.FindAllStringSubmatch(string(data), -1)[0][1]
return token, code
}
func login_lagou() {
//獲取登陸界面的cookie
jar, _ := cookiejar.New(nil)
client := &http.Client{
Jar: jar,
}
req, _ := http.NewRequest("GET", login_url_lagou, nil)
res, _ := client.Do(req)
// for k, v := range res.Cookies() {
// fmt.Printf("%v=%v\n", k, v)
// }
token, code := getToken(res.Body)
fmt.Println(token, code)
//post數(shù)據(jù)
postValues := url.Values{}
postValues.Add("isValidate", "true")
postValues.Add("username", username_lagou)
postValues.Add("password", password_lagou)
postValues.Add("request_form_verifyCode", "")
postValues.Add("submit", "")
// body := ioutil.NopCloser(strings.NewReader(postValues.Encode())) //把form數(shù)據(jù)編下碼
// requ, _ := http.NewRequest("POST", post_login_info_url_lagou, nil)
requ, _ := http.NewRequest("POST", post_login_info_url_lagou, strings.NewReader(postValues.Encode()))
requ.Header.Set("Referer", "https://passport.lagou.com/login/login.html")
requ.Header.Set("X-Requested-With", "XMLHttpRequest")
requ.Header.Set("X-Anit-Forge-Token", token)
requ.Header.Set("X-Anit-Forge-Code", code)
requ.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:51.0) Gecko/20100101 Firefox/51.0")
requ.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
//for _, v := range res.Cookies() {
// requ.AddCookie(v)
//}
res, _ = client.Do(requ)
//cookies_lagou = res.Cookies()
data, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
fmt.Println(string(data))
}
func main() {
login_lagou()
}
運行結(jié)果
15f131a3-74b0-4914-bd6e-2672f36675e1 28747049
{"content":{"rows":[]},"message":"該帳號不存在或密碼(驗證碼)誤,請重新輸入","state":400,"submitCode":23207051,"submitToken":"666f51d4-ccef-462a-bb56-55cb97c5231a"}
Process finished with exit code 0
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團,成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達內(nèi)教育集團成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經(jīng)理職務(wù)負責iOS教學(xué)及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。