importreimportosdef open_file(file='c:/newcrm.html'):
f=open(file,'r',encoding='utf-8')returnfdefwrite_file():
list_api=[]
dict_api={}
file='../test/newcrm_source_api_name.txt'f=open_file()
f.seek(0,0)
str_api_name=re.findall('>.*',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('
请求方式:.*
',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()returnfile#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)))
returnsource_api_name,source_api_path,source_api_methoddefmodify_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('
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 inapi_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 inapi_path_3:if ' ' inpath:
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('
请求方式:',api_method_to_str).group()#匹配字符串开头
api_method_1=re.sub(source_api_method,'',api_method_to_str) #替换为空
source_api_method_2=re.search('
',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))returnnew_file_namedefload_newFile():
new_file='../test/now_newCrm_api.data'
if notos.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=0for a,b,c inzip(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()