python 通过线上API查询ip归属地

API为国外API,频率限制1分钟45个ip

脚本如下

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

import requests,re,json,time,os

base_dir = os.path.dirname(os.path.abspath(__file__))
filename= base_dir + '/ipinfo'
with open(base_dir+'/ipaddress','r') as e:
    ipaddress = e.read()
    ip = json.loads(ipaddress)
ip_count = len(ip)
flag = 0
for x in ip:
    r=requests.get('http://ip-api.com/json/%s'%x)
    count = int(r.headers['X-Rl'])
    ret = r.json()
    info = {'ip':ret['query'],'country':ret['country'],'regionName':ret['regionName'],'city':ret['city'],'AS':ret['as']}

    with open (filename,'a') as a:
        a.write(json.dumps(info,ensure_ascii=False))
        a.write('\n')
    flag+=1
    print('.'*flag,'%d%%'%int(flag/ip_count*100))
    if count < 5:
        print('the limit will run out please wite 60s')
        time.sleep(60)

ipaddress文件为json格式

["103.219.22.126",
"107.155.10.38",
"107.155.16.239",
"107.155.19.39",
"108.61.123.142",
"108.61.170.254",
"108.61.196.36",
"108.61.198.51",
"128.1.55.57",
"146.185.173.26",
"151.80.238.250",
"154.16.202.151",
"172.247.36.37",
"176.126.83.222",
"185.137.181.144",
"185.157.232.103",
"185.175.56.111",
"185.175.58.114"]

ipinfo文件也保存为json格式

{"ip": "107.155.19.39", "country": "Spain", "regionName": "Madrid", "city": "Madrid", "AS": "AS21859 Zenlayer Inc"}
{"ip": "108.61.123.142", "country": "France", "regionName": "Île-de-France", "city": "Paris", "AS": "AS20473 The Constant Company, LLC"}
{"ip": "108.61.170.254", "country": "Germany", "regionName": "Hesse", "city": "Frankfurt am Main", "AS": "AS20473 The Constant Company, LLC"}
{"ip": "108.61.196.36", "country": "United Kingdom", "regionName": "England", "city": "London", "AS": "AS20473 The Constant Company, LLC"}
{"ip": "108.61.198.51", "country": "Netherlands", "regionName": "North Holland", "city": "Amsterdam", "AS": "AS20473 The Constant Company, LLC"}
{"ip": "128.1.55.57", "country": "Russia", "regionName": "Moscow", "city": "Moscow", "AS": "AS21859 Zenlayer Inc"}
{"ip": "146.185.173.26", "country": "Netherlands", "regionName": "North Holland", "city": "Amsterdam", "AS": "AS14061 DigitalOcean, LLC"}
{"ip": "151.80.238.250", "country": "France", "regionName": "Hauts-de-France", "city": "Roubaix", "AS": "AS16276 OVH SAS"}
{"ip": "154.16.202.151", "country": "Germany", "regionName": "Hesse", "city": "Frankfurt am Main", "AS": "AS61317 Digital Energy Technologies Ltd."}
{"ip": "172.247.36.37", "country": "Germany", "regionName": "Hesse", "city": "Frankfurt am Main", "AS": "AS134176 Rainbow network limited"}
{"ip": "176.126.83.222", "country": "Italy", "regionName": "Lombardy", "city": "Milan", "AS": "AS49367 Seflow S.N.C. Di Marco Brame' & C."}
{"ip": "185.137.181.144", "country": "Bulgaria", "regionName": "Varna", "city": "Varna", "AS": "AS3170 Etheroute Ltd"}
{"ip": "185.157.232.103", "country": "United Kingdom", "regionName": "England", "city": "Manchester", "AS": "AS46261 QuickPacket, LLC"}
{"ip": "185.175.56.111", "country": "Norway", "regionName": "Oslo County", "city": "Oslo", "AS": "AS63473 HostHatch, LLC"}
{"ip": "185.175.58.114", "country": "Austria", "regionName": "Vienna", "city": "Vienna", "AS": "AS63473 HostHatch, LLC"}

Leave a Reply

Your email address will not be published. Required fields are marked *

X