python爬蟲 - js逆向之猿人學第十三題cookie驗證


前言

繼續,不多說

 

分析

打開網站:

 

 

然后抓取接口:

 

 

 

查看請求參數,發現沒有什么特別的,就是多了個cookie

 

 

這個cookie咋來的?搜yuanrenxue_cookie搜不到:

 

 

那還是上抓包工具吧,抓包發現了這段js:

 

 

復制出來控制台執行:

 

 這,不用多說了吧,前面復雜的都研究過了,拿到這個去請求就完了

 

 

 

python實現

import requests import execjs headers = { 'accept': 'application/json, text/javascript, */*; q=0.01', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'zh-CN,zh;q=0.9', 'cache-control': 'no-cache', 'pragma': 'no-cache', 'user-agent': 'yuanrenxue.project', 'x-requested-with': 'XMLHttpRequest', 'cookie': 'sessionid=換成你的sessionid' } def get_cookie(): url = 'https://match.yuanrenxue.com/match/13' req = requests.get(url, headers=headers) res = req.content.decode('utf-8') cont = res.replace('<script>document.cookie=','').replace(';location.href=location.pathname+location.search</script>','') req.close() cookie = execjs.eval(cont) return cookie def fetch(page,cc): url = f'https://match.yuanrenxue.com/api/match/13?page={page}' cookie = { 'cookie': f'sessionid=換成你的sessionid; {cc}' } headers.update(cookie) req = requests.get(url, headers=headers) res = req.json() data = res.get('data') data = [temp.get('value') for temp in data] print('temp', data) return data def get_answer(): cookie = get_cookie() sum_number = 0 for i in range(1, 6): cont = fetch(i,cookie) sum_number += sum(cont) print('答案:', sum_number) get_answer()

 

說明下,為哈這里獲取的cookie需要我們自己拼接而不用requests自帶的session對象,因為返回的是js源碼,requests默認支持重定向,然后帶上代理,但是涉及到js代碼的,由於並不是識別js代碼,所以,沒法,得手動處理,唉,前面那么復雜都搞出來了,這里手動處理下無非也就多寫幾行代碼的事

 

執行:

 

 

 

 

答案放上去:

 

 

 

over

 

結語

 

這沒必要多說啥了,第二題都挺過來了,這道題那不是小case

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM