溪水叮咚 发表于 2020-3-29 09:44:53

python——ip定位查询工具

本帖最后由 溪水叮咚 于 2020-3-29 10:14 编辑

用tkinter作为图形界面,re正则表达式第三方库:requestsfrom tkinter import *
import requests
import re


def get_content():
    #获得 输入框中的信息
    ip=ip_input.get()
    #模拟浏览器请求网络
    # headers={'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'}
    headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}


    #请求网络
    response = requests.get("https://www.ipip.net/ip/{}.html".format(ip),headers=headers)
    # print(response.text)
    # print(response.content)
    address = re.search(r'地理位置.*?;">(.*?)</span>',response.text,re.S)
    operator = re.search(r'运营商.*?;">(.*?)</span>', response.text, re.S)
    time_zone = re.search(r'时区.*?;">(.*?)</span>', response.text, re.S)
    wrap = re.search(r'地区.*?;">(.*?)</span>', response.text, re.S)

    if address:
      ip_info=['地理位置:'+address.group(1),'当前的ip:'+ip]
      if operator:
            ip_info.insert(0,'拥有者/运营商:'+operator.group(1))
      if time_zone:
            ip_info.insert(0, '时区:' + time_zone.group(1))
      if wrap:
            ip_info.insert(0, '地区中心经纬度:' + wrap.group(1))

      display_info.delete(0,5)


      for item in ip_info:
            display_info.insert(0,item)
    else:
      display_info.delete(0,5)
      display_info.insert(0,"无效ip")

#创建一个窗口
root=Tk()
#标题
root.title("溪水叮咚的ip定位查询工具")
#设置输入框 规定尺寸
ip_input=Entry(root,width=40)
#创建一个回显列表
display_info=Listbox(root,width=60,height=10)
#创建查询按钮
result_button=Button(root,command=get_content,text="查询")

def main():
    #显示界面
    ip_input.pack()
    display_info.pack()
    result_button.pack()
    #运行
    root.mainloop()

#程序入口
if __name__ == '__main__':
    main()欢迎大家在评论区留言{:10_256:}



页: [1]
查看完整版本: python——ip定位查询工具