zoukankan      html  css  js  c++  java
  • apizza导出为html后,从中提取api_name/api_path/api_method,保存到本地,方便根据接口名称得到接口路径与请求方法

    import re
    import os
    
    def open_file(file='c:/newcrm.html'):
        f=open(file,'r',encoding='utf-8')
        return f
    
    
    def write_file():
        list_api=[]
        dict_api={}
        file='../test/newcrm_source_api_name.txt'
        f=open_file()
        f.seek(0,0)
        str_api_name=re.findall('>.*</h3>',f.read()) #匹配接口名称
        f2=open_file()
        f2.seek(0,0)
        str_api_path=re.findall('请求地址:http://S+w|请求地址:http://s',f2.read())#匹配接口路径
        f3=open_file()
        f3.seek(0,0)
        str_api_method=re.findall('<p>请求方式:.*</p>',f3.read())#匹配接口请求方式
        dict_api['api_name']=str_api_name#将匹配后接口名称插入字典s
        dict_api["api_apth"]=str_api_path#将匹配后接口路径插入字典
        dict_api['api_method']=str_api_method#将匹配后接口请求方式插入字典
        dict_api['api_name'].pop() #删除最后一个
        list_api.append(dict_api)#将字典添加至列表
        # print('来源api_name个数:'+str(len(str_api_name)))
        # print('来源api_path个数:'+str(len(str_api_path)))
        # print('来源api_method个数:'+str(len(str_api_method)))
        new_file=open(file,'w',encoding='utf-8')
        new_file.write(str(list_api))
        f.close()
        f2.close()
        f3.close()
        return file
        # print(dict_api)
        # print(list_api)
    
    def load_file(file=write_file()):#
        str_load=open(file,encoding='utf-8')
        str_api=eval(str_load.read())
        source_api_name=str_api[0]['api_name']
        source_api_path=str_api[0]['api_apth']
        source_api_method=str_api[0]['api_method']
        # print('来源api_name个数:'+str(len(source_api_name)))
        # print('来源api_path个数:'+str(len(source_api_path)))
        # print('来源api_method个数:'+str(len(source_api_method)))
        return source_api_name,source_api_path,source_api_method
    
    def modify_api_nameOrPathOrMethod():
        source_file=load_file()
        api_name=source_file[0]
        api_path=source_file[1]
        api_method=source_file[2]
        '''替换api_name'''
        api_name_to_str=''.join(api_name)
        source_api_name=re.search('^>',api_name_to_str).group()#匹配字符串开头
        api_name_1=re.sub(source_api_name,'',api_name_to_str) #替换为空
        source_api_name_2=re.search('</h3$',api_name_1).group()#匹配字符串结尾
        api_name_2=re.sub(source_api_name_2,'    ',api_name_1)#替换为空格,为了防止匹配结果中带有空格,这里多用几个空格间隔
        api_name_3=re.findall('S+s{4,}',api_name_2)
        api_name_4=[]#存放去掉空格元素后的list
        for name in api_name_3:
            name=name.rstrip()#去掉list元素中的空格
            api_name_4.append(name)
        '''替换api_path'''
        api_path_to_str=''.join(api_path)
        source_api_path=re.search('请求地址:',api_path_to_str).group()#匹配字符串开头
        api_path_1=re.sub(source_api_path,'',api_path_to_str) #替换为空
        source_api_path_2=re.search('http://{{host}}',api_path_1).group()#匹配字符串结尾
        api_path_2=re.sub(source_api_path_2,'  ',api_path_1)#替换为空格
        api_path_3=re.findall('S+|s{3,}',api_path_2)
        api_path_4=[]
        for path in api_path_3:
            if '   ' in path:
                path='api_path为空格:无效路径,位置为列表第%s个元素'%(api_path_3.index(path))
                api_path_4.append(path)
            else:
                api_path_4.append(path)
        '''替换api_method'''
        api_method_to_str=''.join(api_method)
        source_api_method=re.search('<p>请求方式:',api_method_to_str).group()#匹配字符串开头
        api_method_1=re.sub(source_api_method,'',api_method_to_str) #替换为空
        source_api_method_2=re.search('</p>',api_method_1).group()#匹配字符串结尾
        api_method_2=re.sub(source_api_method_2,' ',api_method_1)#替换为空格
        api_method_3=re.findall('S+',api_method_2)
        # 写入数据
        list_api=[]
        dict_api={}
        dict_api['api_name']=api_name_4#将匹配后并处理完毕(去除空格)接口名称插入字典
        dict_api["api_apth"]=api_path_4#将匹配后并处理完毕(对路径为空格的进行说明)接口路径插入字典
        dict_api['api_method']=api_method_3#将匹配后接口请求方式插入字典
        list_api.append(dict_api)#将字典添加至列表
        # print('最终api_name个数:'+str(len(api_name_4))) 
        # print('最终api_path个数:'+str(len(api_path_4)))
        # print('最终api_method个数:'+str(len(api_method_3)))
        new_file_name='../test/now_newCrm_api.data'
        new_file=open(new_file_name,'w',encoding='utf-8')
        new_file.write(str(list_api))
        return new_file_name
    
    
    
    def load_newFile():
        new_file='../test/now_newCrm_api.data'
        if not os.path.exists(new_file):
            new_file=modify_api_nameOrPathOrMethod()
        new_str_load=open(new_file,encoding='utf-8')
        new_str_api=eval(new_str_load.read())
        new_api_name=new_str_api[0]['api_name']
        new_api_path=new_str_api[0]['api_apth']
        new_api_method=new_str_api[0]['api_method']
        # print('来源api_name个数:'+str(len(new_api_name)))
        # print('来源api_path个数:'+str(len(new_api_name)))
        # print('来源api_method个数:'+str(len(new_api_name)))
        count=0
        for a,b,c in zip(new_api_name,new_api_path,new_api_method):
            # if len(new_api_name)==5:
            count+=1
            if count<5:
                print(a,b,c)
    
    # write_file()
    # load_file()
    # test=modify_api_nameOrPathOrMethod()
    load_newFile()
  • 相关阅读:
    多库查询 sp_addlinkedserver使用方法(添加链接服务器)(转)片段整理
    利用asp.net路由实现url解析
    C#事务 访问数据库(转)
    男人30而立,30岁的男人喊起来!你们立了吗?
    c# 调用SQL Server存储过程返回值(转)
    转摘 JQUERY操作JSON例子
    jstree 从简单说起Jquery 插件应用说明
    利用反射对对象属性赋值取值操作
    asp.net 造成seesion 丢失的问题之一
    jquery 实现从左边listbox选择至右边listbox
  • 原文地址:https://www.cnblogs.com/qtclm/p/10808954.html
Copyright © 2011-2022 走看看