加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

超好用的ip归属区域查询

发布时间:2020-12-14 03:18:37 所属栏目:大数据 来源:网络整理
导读:源码如下: #!/usr/bin/env python #-*-coding:utf-8-*- import requests from bs4 import BeautifulSoup import re import IPy def check_ip(Ip): url = ‘https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?co=resource_id=6006t=1529895387942ie=u

源码如下:

#!/usr/bin/env python
#-*-coding:utf-8-*-

import requests
from bs4 import BeautifulSoup
import re
import IPy

def check_ip(Ip):
url = ‘https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?co=&resource_id=6006&t=1529895387942&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&cb=jQuery110203920624944751099_1529894588086&_=1529894588088&query=‘ + Ip
headers = {‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/67.0.3396.87 Safari/537.36‘}
r = requests.get(url,headers=headers)
r.encoding = r.apparent_encoding
html = r.text
c1 = html.split(‘location":"‘)[1]
c2 = c1.split(‘",‘)[0]
return c2

#判断ip地址是否正确
def is_ip(address):
try:
IPy.IP(address)
return True
except Exception as e:
return False

# 获取外网IP
def get_out_ip():
#url1 = ‘https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?co=&resource_id=6006&t=1529895387942&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&cb=jQuery110203920624944751099_1529894588086&_=1529894588088&query=‘ + Ip
#headers = {‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/67.0.3396.87 Safari/537.36‘}
url = ‘http://ip.42.pl/raw‘
r = requests.get(url)
result = r.text
return result

#检查IP地址归属地
def checkIP():
Ip = input("Please input a IP(按回车键退出):")
judge = is_ip(Ip)
if judge:
length = len(Ip.split(‘.‘))
if length == 4:
c2 = check_ip(Ip)
result = Ip + ‘来自‘ + c2
return result
elif Ip.strip() == ‘‘:
exit(2)

# return (‘IP输入错误,请重新输入!‘)
else:
return (‘Ip输入错误,请重新输入!‘)

if __name__ == ‘__main__‘:
Ip = get_out_ip()
print(‘本机IP:‘ + Ip + ‘ ‘ + check_ip(Ip))
while True:
print(checkIP())

可以打包成windwos的可执行exe格式,通过软件pyinstaller软件效果如下图:

?

?

此处为单个ip查询,也可以自行修改脚本同时查询多个。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读