• python接口自动化测试二十:函数写接口测试


    # coding:utf-8
    import requests
    import re
    from bs4 import BeautifulSoup

    # s = requests.session() # 全局的s

    def get_token(s):
    '''
    fuction: 获取token
    args: s 参数 -》s = requests.session()
    :return anti_token ->{'X-Anit-Forge-Token': 'xx', 'X-Anit-Forge-Code': '38515842'}
    '''
    # 局部的s没定义,从外部传入s
    url = 'https://passport.lagou.com/login/login.html'
    h1 = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0"
    }
    r1 = s.get(url, headers=h1, verify=False)
    # print(r1.text)

    soup = BeautifulSoup(r1.content, "html.parser", from_encoding='utf-8')
    tokenCode = {}
    try:
    t = soup.find_all('script')[1].get_text()
    print(t)
    tokenCode['X_Anti_Forge_Token'] = re.findall(r"Token = '(.+?)'", t)[0]
    tokenCode['X_Anti_Forge_Code'] = re.findall(r"Code = '(.+?)'", t)[0]
    return tokenCode
    except:
    print("获取token和code失败")
    tokenCode['X_Anti_Forge_Token'] = ""
    tokenCode['X_Anti_Forge_Code'] = ""
    return tokenCode


    def login_lgw(s, anti_token, user, psw):

    url2 = 'https://passport.lagou.com/login/login.json'
    h2 = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "X-Requested-With": "XMLHttpRequest",
    "X-Anit-Forge-Token": anti_token['X_Anti_Forge_Token'] ,
    "X-Anit-Forge-Code": anti_token['X_Anti_Forge_Code'],
    "Referer": "https://passport.lagou.com/login/login.html"
    }
    body ={
    "isValidate": "true",
    "username": user,
    "password": psw,
    "request_form_verifyCode": "",
    "submit": ""
    }
    print(s.headers) # s的头部

    # 更新s的头部
    s.headers.update(h2)
    print(s.headers)
    r2 = s.post(url2, data=body, verify=False)
    print(r2.text)
    return r2.json()

    if __name__ == "__main__":
    # 自测的内容
    s = requests.session()
    token = get_token(s)
    print(token)
    user = "1232"
    psw = "322222"
    login_lgw(s, token, user, psw)


    
    
  • 相关阅读:
    Java系列教程-Spring 教程目录
    python中如何给散点图上面的特定点做标记
    OpenCV:Python下OpenCV安装和入门最强详细攻略
    如何实现一个 windows 桌面动态壁纸
    学习C/C++的简单方法
    手眼标定
    python合并多个txt文件
    python qq发消息
    python获取当前天气情况
    程序代写、毕业设计
  • 原文地址:https://www.cnblogs.com/zhongyehai/p/9160191.html
Copyright © 2020-2023  润新知