等号对2字典

 方法一:使用py脚本

实现2种功能:

(1)cookie中,等号转为冒号。

(2)请求中,冒号添加引号,删除首位冒号。

def denghao2maohao(cookie_str):
    # 截断数据对
    list1 = cookie_str.split(";")
    # print(list1)
    # 初始化字典
    cookie_dict_str = {}
    for item in list1:
        list2 = item.split("=", 1)  # 按照等号只切割一次
        # print(list2)
        dict_key = list2[0].strip()
        dict_value = list2[1].strip()
        cookie_dict_str[dict_key] = dict_value
    return cookie_dict_str


def maohao2yinhao(maohao_str):
    list1 = maohao_str.strip().splitlines()
    maohao_str_dict = {}
    for item in list1:
        if item.strip().startswith(":"):
            # print(item.strip())
            list2 = item.strip().split(":", 2)  # 按照分号截断2次
            # print(list2)
            new_key = list2[1]
            new_value = list2[2].strip()
            # maohao_str_dict[":" + new_key] = new_value  # 保留首冒号
            maohao_str_dict[new_key] = new_value  # 删除首冒号
            print("'%s':'%s'," % (new_key, new_value))
        else:
            # print(item)
            list2 = item.split(":", 1)  # 按照分号截断1次
            maohao_str_dict[list2[0].strip()] = list2[1].strip()

            new_key = list2[0].strip()
            new_value = list2[1].strip()
            maohao_str_dict[new_key] = new_value
            print("'%s':'%s'," % (new_key, new_value))  # 输出格式化好的键值对

    return maohao_str_dict


if __name__ == '__main__':
    # # cookie中,等号转为冒号
    # cookie_str = "ss_lang=cs; product=WGSN; ss_udid=0ed9a26e6dd6bb892c796cda69bca4a3; PHPSESSID=ci56j78njjgdqde5tjepslaah5; exclusionChecked=True; ss_token=f77dcbc5a65f43977e02b61e9d6ff947; trwv.uid=stylesight-1525165098107-fd45157e%3A2; trwsa.sid=stylesight-1525177471085-3d01fa38%3A2; _ga=GA1.2.1824486173.1525165097; _gid=GA1.2.1794994253.1525165097; cp_browStat=Logged In; cp_UserID=-1; cp_hybridBrowStat=Logged In; cp_SubStat=Subscriber"
    # # print(cookie_str)
    # cookie_dict_str = denghao2maohao(cookie_str)
    # print("======【1】 cookie等号转为冒号  ========")
    # print(cookie_str)
    # print()
    # print(cookie_dict_str)

    # 请求中,冒号添引号,删除首冒号
    maohao_str = """
    :authority:www.wgsnchina.cn
    :method:POST
    :path:/api/cherry/search/query
    :scheme:https
    accept:application/json, text/plain, */*
    accept-encoding:gzip, deflate, br
    accept-language:zh-CN,zh;q=0.9
    content-length:149
    content-type:application/json;charset=UTF-8
    cookie:ss_lang=cs; product=WGSN; ss_udid=0ed9a26e6dd6bb892c796cda69bca4a3; PHPSESSID=ci56j78njjgdqde5tjepslaah5; exclusionChecked=True; ss_token=f77dcbc5a65f43977e02b61e9d6ff947; _gat_UA-1004012-2=1; cp_SubStat=Subscriber; cp_browStat=Logged In; cp_UserID=-1; cp_hybridBrowStat=Logged In; _dc_gtm_UA-1004012-2=1; _ga=GA1.2.1824486173.1525165097; _gid=GA1.2.1794994253.1525165097; trwv.uid=stylesight-1525165098107-fd45157e%3A3; trwsa.sid=stylesight-1525179968287-e61a7bc2%3A2
    origin:https://www.wgsnchina.cn
    referer:https://www.wgsnchina.cn/library/results/ab745207e8ed3dcfa16b4814748beead
    user-agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36
    """
    print("======【2】 请求中,冒号添引号,删除首冒号  ========")
    maohao_str_dict = maohao2yinhao(maohao_str)
    # print(maohao_str)
    print()
    print(maohao_str_dict)

  

方法二:使用postman

https://zhuanlan.zhihu.com/p/42832499

posted @ 2018-05-01 20:54  安迪9468  阅读(284)  评论(0编辑  收藏  举报