python爬蟲 - js逆向之猿人學第十三題
阿新 • • 發佈:2021-09-16
繼續,不多說
分析
開啟網站:
然後抓取介面:
檢視請求引數,發現沒有什麼特別的,就是多了個cookie
這個cookie咋來的?搜yuanrenxue_cookie搜不到:
那還是上抓包工具吧,抓包發現了這段js:
這,不用多說了吧,前面複雜的都研究過了,複製出來控制檯執行:
ok,拿到這個去請求就完了
前言
繼續,不多說
分析
開啟網站:
然後抓取介面:
檢視請求引數,發現沒有什麼特別的,就是多了個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