#准備工具,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()