Requests方法 -- Token獲取操作


獲取token和code流程如下:
a、先登陸抓包查看post(提交表單操作)頭中是否有token和code關鍵字
b、已知步驟a中出現了token和code,不登錄前刷新登陸頁面,查看response中是否有token和code關鍵字
c、輸入賬號密碼后查看登陸后的請求頭中token和code是否與未登陸前response中的一致(鎖定后就好操作了)

1、打開登錄首頁https://passport.lagou.com/login/login.html,直接按F5刷新(只做刷新動作,不輸入賬號和密碼),然后從返回的頁面找到token生成的位置

2、輸入賬號和密碼登錄,查看post請求后中是否帶有token和code,與未登陸前的一致,說明token和code在登陸頁面傳入到請求頭中。

 

3、代碼參考如下:

import requests,re,urllib3,hashlib
from bs4 import BeautifulSoup
urllib3.disable_warnings()

class LoginLgw():
def __init__(self,s):
self.s = s

def getTokenCode(self):
"""
要從登錄頁面提取token,code, 然后在頭信息里面添加
<!-- 頁面樣式 --> <!-- 動態token,防御偽造請求,重復提交 -->
<script>
window.X_Anti_Forge_Token = 'b792db29-d4d3-484e-98b4-04bbe0f628fe';
window.X_Anti_Forge_Code = '36611432';
</script>
"""
url = "https://passport.lagou.com/login/login.html"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
}
# 更新session的headers
self.s.headers.update(headers)
data = self.s.get(url,verify=False)
soup = BeautifulSoup(data.content,"html.parser")
tokenCode = {}
try:
token = soup.find_all("script")[1].get_text()
print(token)
tokenCode['X_Anti_Forge_Token'] = re.findall(r"Token = '(.+?)'",token)[0]
tokenCode['X_Anti_Forge_Code'] = re.findall(r"Code = '(.+?)'",token)[0]
return tokenCode
except:
print("獲取token和code失敗")
tokenCode['X_Anti_Forge_Token'] = ""
tokenCode['X_Anti_Forge_Code'] = ""
return tokenCode

def encryptPwd(self, passwd):
# 對密碼進行了md5雙重加密
passwd = hashlib.md5(passwd.encode('utf-8')).hexdigest()
# veennike 這個值是在js文件找到的一個寫死的值
passwd = 'veenike' + passwd + 'veenike'
passwd = hashlib.md5(passwd.encode('utf-8')).hexdigest()
return passwd

def login(self, user, psw):
'''
function:登錄拉勾網網站
:param user: 賬號
:param psw: 密碼
:return: 返回json
'''
gtoken = self.getTokenCode()
print(gtoken)
print(gtoken['X_Anti_Forge_Token'])
print(gtoken['X_Anti_Forge_Code'])
url2 = "https://passport.lagou.com/login/login.json"
headers2 = {
"X-Anit-Forge-Code": gtoken['X_Anti_Forge_Code'],
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Accept": "application/json, text/javascript, */*; q=0.01",
"X-Requested-With": "XMLHttpRequest",
"X-Anit-Forge-Token": gtoken['X_Anti_Forge_Token'],
"Referer": "https://passport.lagou.com/login/login.html"
}
# 更新s的頭部
self.s.headers.update(headers2)
passwd = self.encryptPwd(psw)

body = {
"isValidate":"true",
"username": user,
"password": passwd,
"request_form_verifyCode":"",
"submit":"",
"challenge":"8e2eabfd601c8ae65e536c327679d99c"
}

r2 = self.s.post(url2,headers=headers2,data=body,verify=False)
try:
print(r2.text)
return r2.json()
except:
print("登錄異常信息:%s" % r2.text)
return None

if __name__ == "__main__":
s = requests.Session()
lgw = LoginLgw(s)
lgw.login("賬號", "密碼")

 


免責聲明!

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



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