禅道登录流程

  1. 打开登陆页面的时候页面产生一个zentaosid的唯一标识,以及一个
  2. verifyRand随机值
  3. 对输入的密码进行md5加密
  4. 已经加密的密码+verifyRand再一次md5加密
  5. 然后post请求登陆地址根据唯一的zentaosid验密

获取cookie

import hashlib

import requests
from lxml import html

def get_cookie():
    loginUrl = '禅道登录地址'
    password = '密码'
    loginName = '用户名'
    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)
    loginResultPage.encoding = 'utf-8'
    # print('loginResultPage = ' + loginResultPage.text)
    cookie = loginResultPage.cookies['zp']
    # print('token = ' + cookie)
    return cookie

爬取禅道bug

def get_bug_list():
    url = 'host地址/zentao/report-bugcreate.html'
    payload = {}
    cookie = get_cookie()
    cookies = {
        'za': '用户名',
        'zp': cookie,
    }

    response = requests.request("GET", url=url, data=payload, cookies=cookies)
    response.encoding = 'utf-8'  # 设置编码格式为utf-8
    soup = BeautifulSoup(response.text, 'html.parser')  # 开始解析
    links_th = soup.find_all('tr')  # 获取所有的tr标签形成列表
    bug_list = []
    list_name = []  # 需要统计的人员姓名
    for i in links_th[1:]:
        text = (i.find_all("td"))  # 获取tr标签下所有td标签形成列表
        dic = {
            # 解析获取text信息
            "name": text[0].get_text(),  # 创建人名称
            'rate': text[10].get_text(),  # 有效率
            'total': text[11].get_text(),  # 总计
        }
        if dic.get('name') in list_name:
            dic['bug_amount'] = int(float(dic['rate'][:-1]) / 100 * int(dic['total']))   # 有效bug数
            bug_list.append(dic)
    return bug_list

参考文章:https://blog.csdn.net/weixin_43186603/article/details/85245892

Q.E.D.


Nothing really matters to me!