Python 收货地址自动识别(使用接口)

#准备工具,http://www.flagyun.com/developer.html#queryaddress 注册并获取接口appkey


import tkinter as tk import json import requests import urllib import ctypes #隐藏控制台窗口 whnd = ctypes.windll.kernel32.GetConsoleWindow() if whnd != 0: ctypes.windll.user32.ShowWindow(whnd, 0) ctypes.windll.kernel32.CloseHandle(whnd) #处理接口请求并返回指定参数 def run( Text): url = 'https://open.flagyun.com/queryaddress.ashx' #heads = {'Content-type': 'application/x-www-form-urlencoded'} # 封装传入的参数 m_post_data = {} m_post_data['appkey'] = '替换注册的appkey' #这里的key使用提供注册的的appkey!!!! m_post_data['address'] = urllib.parse.quote(Text) #使用汉字编码 url = url + '?' + 'appkey=这里输入appkey&address=' + m_post_data['address'] #请求的参数进行拼接 这里需要更换appkey在进行拼接!!!! data= '' uuu = requests.post(url,data) #请求数据并返回 d = json.loads(uuu.text) re_request = d['resultList'] if len(re_request) == 0 : #messagebox.showinfo(title='提示', message='参数address过短,无需解析!') return 'none' else: f = d['resultList'] g = f[0]['province'] h = f[0]['city'] i = f[0]['area'] j = f[0]['details'] k = f[0]['name'] l = f[0]['mobile'] strcat = g+'\t' + h +'\t' + i +'\t' + j + '\t' + k + '\t' + l + '\n' return strcat # 处理後的数据写入电子表格中 def Write_File(Text): infile = open('Data.xls', 'a+') infile.write(Text) infile.close() def not_auto_run(): str = m_Show_Text.get() Write_File(str) # 将未处理的数据存入缓冲区 def Read_File(): outfile = open('config.txt', 'r') StrList = {} for line in outfile.readlines(): StrList[line] = line else: #messagebox.showinfo(title='提示', message='数据处理完毕!') return StrList #配置文件读取,自动处理 def auto_run(): List = Read_File() for index in List: str = run(index) if str== 'none': #判断是否有数据为空,否则不予处理! continue else: Write_File(str) #界面展示 top = tk.Tk() top.geometry('800x600') m_Input_Edit= tk.Entry(top, width = 100, fg='red') m_Input_Edit.pack() m_Show_Text=tk.Entry(top,width = 100) m_Show_Text.pack() #处理数据 def get(): str = m_Input_Edit.get() #获取地址 tospringlite = run(str) m_Show_Text.delete(0, 1024) m_Show_Text.insert(0, tospringlite) m_Input_Edit.delete(0, 1024) B = tk.Button(top, text ="数据处理",command = get) B.pack() run_button = tk.Button(top, text ="手动处理,将处理後的文件放入指定文件中",command = not_auto_run) run_button.pack() #自动处理按钮 autorun_button = tk.Button(top, text ="数据自动处理",command = auto_run) autorun_button.pack() top.mainloop()

  

posted @ 2021-06-01 23:29  神迹丶  阅读(853)  评论(0编辑  收藏  举报
网站已运行: