尝试用python/requests偷懒(1)-拼接信息写测试测试报告

背景:

每次发版需要写测试报告,报告中有一项是关于版本号的信息,需要将公司平台页面的表格内容,拼接、粘贴到word。

如图

需求:

将以上图片信息组合成以下文本格式:

系统名称+版本号/文件路径(图片列)

MD5码:MD5码(图片列)

注:系统名称+版本号抓包后在response中可获取

解决代码:

import requests

# 网上找的处理requests库返回json类型(在python接收为多维字典)的方法
def get_target_value(key, dic, tmp_list):
    """
    :param key: 目标key值
    :param dic: JSON数据
    :param tmp_list: 用于存储获取的数据
    :return: list
    """
    if not isinstance(dic, dict) or not isinstance(tmp_list, list):  # 对传入数据进行格式校验
        return 'argv[1] not an dict or argv[-1] not an list '

    if key in dic.keys():
        tmp_list.append(dic[key])  # 传入数据存在则存入tmp_list

    for value in dic.values():  # 传入数据不符合则对其value值进行遍历
        if isinstance(value, dict):
            get_target_value(key, value, tmp_list)  # 传入数据的value值是字典,则直接调用自身
        elif isinstance(value, (list, tuple)):
            _get_value(key, value, tmp_list)  # 传入数据的value值是列表或者元组,则调用_get_value


    return tmp_list


def _get_value(key, val, tmp_list):
    for val_ in val:
        if isinstance(val_, dict):
            get_target_value(key, val_, tmp_list)  # 传入数据的value值是字典,则调用get_target_value
        elif isinstance(val_, (list, tuple)):
            _get_value(key, val_, tmp_list)   # 传入数据的value值是列表或者元组,则调用自身

# 获取版本信息方法
def get_reportinfo(ywpturl, authinfo):
# 必要的header信息
    ywptheaders = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36',
        'Cookie': authinfo
    }
# 调用查看版本列表接口
    r = requests.get(ywpturl, headers=ywptheaders)
# 将接口返回的列表存储,知识点,返回结果在python为字典类型,此字典为多维字典
    resraw = r.json()
# 用网上方法获取字典的值,存到数组
    md5s = get_target_value('md5',resraw,[])
    paths = get_target_value('filedirectory',resraw,[])
    filenames = get_target_value('filename',resraw,[])
    sysnames = get_target_value('sysname',resraw,[])

    print("最终结果是----------------------------------------------------------------------")
# 拼接、组合以上数组,打印输出
    for path, filename, md5, sysname in zip(paths, filenames, md5s, sysnames):
        print(sysname+path+'/'+filename)
        print("MD5码:", md5)

# 设置变量,接收每次版本的请求地址和登录态
url = "http://abc.com/pub/Excel?ftptype=21.0.134.99&morepaths=666666666E766%%%%%86%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F"
cookie = 'this.is.token.SESSIONID=youcannotsee'

# 调用获取版本信息方法
get_reportinfo(url, cookie)

结果:

 

posted @ 2022-05-27 11:14  hello_mercy  阅读(50)  评论(0编辑  收藏  举报