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

个人博客地址

http://www.darkghost.life

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

脚本如下

 1 #!/usr/bin/env python3
 2 #-*-coding:utf-8-*-
 3 
 4 import requests,re,json,time,os
 5 
 6 base_dir = os.path.dirname(os.path.abspath(__file__))
 7 filename= base_dir + '/ipinfo'
 8 with open(base_dir+'/ipaddress','r') as e:
 9     ipaddress = e.read()
10     ip = json.loads(ipaddress)
11 ip_count = len(ip)
12 flag = 0
13 for x in ip:
14     r=requests.get('http://ip-api.com/json/%s'%x)
15     count = int(r.headers['X-Rl'])
16     ret = r.json()
17     info = {'ip':ret['query'],'country':ret['country'],'regionName':ret['regionName'],'city':ret['city'],'AS':ret['as']}
18     
19     with open (filename,'a') as a:
20         a.write(json.dumps(info,ensure_ascii=False))
21         a.write('\n')
22     flag+=1
23     print('.'*flag,'%d%%'%int(flag/ip_count*100))
24     if count < 5:
25         print('the limit will run out please wite 60s')
26         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"}

 

posted @ 2021-05-18 18:51  无限's-blog  阅读(201)  评论(0编辑  收藏  举报