ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

用python从IP138通过HTML方法得到自己的IP

2021-10-01 21:33:31  阅读:158  来源: 互联网

标签:ip real python IP window HTML text import div


# 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()

标签:ip,real,python,IP,window,HTML,text,import,div
来源: https://blog.csdn.net/qq_43477640/article/details/120581048

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有