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()