import requests
import bs4
import ctypes
import tkinter
import tkinter.ttk
import tkinter.scrolledtext
import pyperclip
import lxml
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 '
'YaBrowser/19.7.0.1635 Yowser/2.5 Safari/537.36',
}
def get_ip():
index_response = requests.get("https://ip138.com/", headers=headers)
index_soup = bs4.BeautifulSoup(index_response.text, "lxml")
real_ip_info_domain = index_soup.select(
"body > div > div.container > div.content > div > div.mod-search > div.hd > "
"iframe")[0].attrs['src'].replace('/', '')
try:
real_ip_info_response = requests.get("http://" + real_ip_info_domain + "/", headers=headers)
except:
IP_Label.configure(text='获取太快被网站暂时禁止')
else:
real_soup = bs4.BeautifulSoup(real_ip_info_response.text, "lxml")
ip_address = real_soup.select('body > p:nth-child(1) > a')[0].text
IP_Label.configure(text=ip_address)
window = tkinter.Tk()
ctypes.windll.shcore.SetProcessDpiAwareness(1)
ScaleFactor = ctypes.windll.shcore.GetScaleFactorForDevice(0)
window.tk.call('tk', 'scaling', ScaleFactor / 72)
window.title('IP138获取IP_HTML方式')
confirm = tkinter.ttk.Button(window, text="刷新", command=get_ip)
IP_Label = tkinter.ttk.Label(window)
IP_Label.grid(column=0, row=0)
confirm.grid(column=1, row=0)
get_ip()
window.mainloop()
|