init
拉勾搜索頁(yè)面 : https://www.lagou.com/jobs/list_golang?city=%E6%9D%AD%E5%B7%9E&cl=false&fromSearch=true&labelWords=&suginput=
Ajax異步請(qǐng)求的頁(yè)面: https://www.lagou.com/jobs/positionAjax.json?city=%E6%9D%AD%E5%B7%9E&needAddtionalResult=false&isSchoolJob=0
糗事百科:https://www.qiushibaike.com/
問題描述
- Ajax異步加載。拉勾搜索頁(yè)面的source page 不像 糗事百科的 source page,沒有要抓取的那些信息(如“GO開發(fā)工程師 1天前發(fā)布 10k-20k 經(jīng)驗(yàn)1-3年 / 大專”),這些信息是Ajax異步加載的;對(duì)于異步加載的搜索結(jié)果頁(yè),無(wú)法像糗事百科那樣直接從source page中匹配想要的結(jié)果。
- 模仿瀏覽器發(fā)送post請(qǐng)求。Ajax異步請(qǐng)求的網(wǎng)址,利用postman發(fā)送post請(qǐng)求,總是返回系統(tǒng)繁忙。
解決辦法
問題1:google 瀏覽器按F12,按一下一個(gè)空心圓帶斜杠的按鈕(“clear”),先將頁(yè)面的結(jié)果清楚,然后刷新頁(yè)面,然后查看加載出來(lái)的那些網(wǎng)址的Header,在General中Request Method為POST的,差不多就是Ajax請(qǐng)求數(shù)據(jù)的真正地址。找到這個(gè)地址后,看“Preview”,就能查看response的格式。
問題2:
- 端正態(tài)度。其實(shí)找到Ajax請(qǐng)求的真正地址后,用postman就應(yīng)該能模擬出這個(gè)Ajax請(qǐng)求,我之所以失敗,是因?yàn)槲姨珣辛?,只填了幾個(gè)Request Headers。后來(lái)我把那些Request Headers中的項(xiàng)全添加到Postman中,就加載出來(lái)了。然后這個(gè)事情啟發(fā)了我,只要把Request Headers/Query String Parameters/Form Data 全部實(shí)現(xiàn),無(wú)論使用什么,都應(yīng)該返回正確的結(jié)果才對(duì)。
- FormData 要注意一下數(shù)據(jù)格式,一般來(lái)說Ajax默認(rèn)請(qǐng)求的格式,是“application/x-www-form-urlencoded”。由此我產(chǎn)生了一個(gè)疑問,post都有幾種數(shù)據(jù)格式?對(duì)于這個(gè)問題,我找到一篇很好的文章,https://imququ.com/post/four-ways-to-post-data-in-http.html。
代碼實(shí)現(xiàn)
type RequestInfo struct {
Url string
Data map[string]string //post要傳輸?shù)臄?shù)據(jù),必須key value必須都是string
DataInterface map[string]interface{}
}
//適用于 application/x-www-form-urlencoded
func (this RequestInfo) postUrlEncoded()([]byte,error){
client := &http.Client{}
//post要提交的數(shù)據(jù)
DataUrlVal := url.Values{}
for key,val := range this.Data{
DataUrlVal.Add(key,val)
}
req,err := http.NewRequest("POST",this.Url,strings.NewReader(DataUrlVal.Encode()))
if err != nil{
return nil,err
}
//偽裝頭部
req.Header.Set("Accept","application/json, text/javascript, */*; q=0.01")
req.Header.Add("Accept-Encoding","gzip, deflate, br")
req.Header.Add("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4")
req.Header.Add("Connection","keep-alive")
req.Header.Add("Content-Length","25")
req.Header.Add("Content-Type","application/x-www-form-urlencoded")
req.Header.Add("Cookie","user_trace_token=20170425200852-dfbddc2c21fd492caac33936c08aef7e; LGUID=20170425200852-f2e56fe3-29af-11e7-b359-5254005c3644; showExpriedIndex=1; showExpriedCompanyHome=1; showExpriedMyPublish=1; hasDeliver=22; index_location_city=%E5%85%A8%E5%9B%BD; JSESSIONID=CEB4F9FAD55FDA93B8B43DC64F6D3DB8; TG-TRACK-CODE=search_code; SEARCH_ID=b642e683bb424e7f8622b0c6a17ffeeb; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1493122129,1493380366; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1493383810; _ga=GA1.2.1167865619.1493122129; LGSID=20170428195247-32c086bf-2c09-11e7-871f-525400f775ce; LGRID=20170428205011-376bf3ce-2c11-11e7-8724-525400f775ce; _putrc=AFBE3C2EAEBB8730")
req.Header.Add("Host","www.lagou.com")
req.Header.Add("Origin","https://www.lagou.com")
req.Header.Add("Referer","https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=")
req.Header.Add("X-Anit-Forge-Code","0")
req.Header.Add("X-Anit-Forge-Token","None")
req.Header.Add("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36")
req.Header.Add("X-Requested-With","XMLHttpRequest")
//提交請(qǐng)求
resp,err := client.Do(req)
defer resp.Body.Close()
if err != nil{
return nil,err
}
//讀取返回值
result,err := ioutil.ReadAll(resp.Body)
if err != nil{
return nil,err
}
return result,nil
}