1. 程式人生 > >用爬蟲登陸禪道最新版

用爬蟲登陸禪道最新版

公司有個內部的專案管理軟體,其中任務和bug模組需要從禪道發起,然後用爬蟲爬取禪道的任務和bug列表及詳細資訊,從而需要獲取禪道的登陸token
經過研究,發現禪道的登陸流程如下:

  1. 開啟登陸頁面的時候頁面產生一個zentaosid的唯一標識,以及一個verifyRand隨機值
  2. 對輸入的密碼進行md5加密
  3. 已經加密的密碼+verifyRand再一次md5加密
  4. 然後post請求登陸地址根據唯一的zentaosid驗密

知道了以上步驟不難寫出爬蟲程式碼,以python為例

import hashlib

import requests
from
lxml import html loginUrl = '禪道的登陸地址' password = '密碼' loginName = '使用者名稱' def getToken(): loginPage = requests.get(loginUrl) loginPage.encoding = 'utf-8' SID = loginPage.cookies['zentaosid'] print('SID = ' + SID) loginTree = html.fromstring(loginPage.text) verifyRand = loginTree.
xpath('//*[@id="verifyRand"]') if verifyRand: verifyRand = verifyRand[0].attrib['value'] print('verifyRand = ' + verifyRand) hl = hashlib.md5() hl.update(password.encode(encoding='utf-8')) print('Md5 第一次加密結果 = ' + hl.hexdigest()) passwordResult = hl.hexdigest() + verifyRand print
("passwordResult=" + passwordResult) hlLast = hashlib.md5() hlLast.update(passwordResult.encode(encoding='utf-8')) print('Md5 第二次加密結果 = ' + hlLast.hexdigest()) loginBody = {"account": loginName, "password": hlLast.hexdigest(), "keepLogin[]": "on"} loginCookies = dict(zentaosid=SID, lang='zh-cn', keepLogin='on') loginResultPage = requests.post(loginUrl, data=loginBody, cookies=loginCookies) print('loginResultPage = ' + loginResultPage.text) token = loginResultPage.cookies['zp'] print('token = ' + token) return token

獲取token之後就可以愉快的爬取自己需要的資訊了