from tkinter import *
import requests
import re

def get_concent():
    ip=ip_input.get()  #获取输入框内容
    url="https://www.ipip.net/ip/{}.html".format(ip)
    #设置请求头 模拟浏览器
    headers={
       "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
    }
    #请求网络 并解码
    html=requests.get(url=url,headers=headers).content.decode()
    #print(html)
    address=re.search(r'地理位置.*?;">(.*?)</span>',html,re.S)
    operator = re.search(r'运营商.*?;">(.*?)</span>', html, re.S)
    time = re.search(r'时区.*?;">(.*?)</span>', html, re.S)
    wrap = re.search(r'地区中心纬度.*?;">(.*?)</span>', html, re.S)
    print(address.group(1))

    if address:
        ip_info=["地理位置:"+address.group(1),"当前IP:"+ip]
        if operator:
            ip_info.insert(0,'所有者、运营商:'+operator.group(1))
        if time:
            ip_info.insert(0, '时区:' + time.group(1))
        if wrap:
            ip_info.insert(0, '地区中心纬度:' + wrap.group(1))
        #清空数据
        display_info.delete(0,5)

        #为GUI列表赋值
        for item in ip_info:
            display_info.insert(0,item)

    else:
        display_info.delete(0,5)
        display_info.insert(0,"这是一个无效的ip地址.")


tk=Tk()    #构建窗口
tk.title("ip地址")  #设置标题
ip_input=Entry(tk,width=15)   #输入框
display_info=Listbox(tk,width=50,height=10)  #构建一个回显列表
result_button=Button(tk,command=get_concent,text="查询")   #按钮事件


#程序入口
if __name__=='__main__':
    # 显示
    ip_input.pack()
    display_info.pack()
    result_button.pack()
    # 运行
    tk.mainloop()

 

posted on 2019-01-15 14:43  -豪-  阅读(218)  评论(0)    收藏  举报