本文主要是介绍用python从IP138通过HTML方法得到自己的IP,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
# code at 2021-10-1
# 根据html标签获取数据
# 需要安装包pip lxml
import requests
import bs4
import ctypes
import tkinter
import tkinter.ttk
import tkinter.scrolledtext
import pyperclip
import lxml
headers = {
# 'authority': 'developer.mozilla.org',
# 'pragma': 'no-cache',
# 'cache-control': 'no-cache',
# 'upgrade-insecure-requests': '1',
'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',
# 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,
# application/signed-exchange;v=b3', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'zh-CN,
# zh-TW;q=0.9,zh;q=0.8,en-US;q=0.7,en;q=0.6', 'cookie': 你的cookie,
}
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()
这篇关于用python从IP138通过HTML方法得到自己的IP的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!