用python獲取ip信息


 
 
1.138網站

http://user.ip138.com/ip/
首次注冊后贈送1000次請求,API接口請求格式如下,必須要有token值

1 import httplib2 2 from urllib.parse import urlencode #python3
3 #from urllib import urlencode #python2
4 params = urlencode({'ip':'8.8.8.8','datatype':'jsonp','callback':'find'}) 5 url = 'http://api.ip138.com/query/?'+params 6 headers = {"token":"8594766483a2d65d76804906dd1a1c6a"}#token為示例
7 http = httplib2.Http() 8 response, content = http.request(url,'GET',headers=headers) 9 print(content.decode("utf-8"))

響應格式

 

{ "ret":"ok", // ret 值為 ok 時 返回 data 數據 為err時返回msg數據 "ip":"114.114.114.114", // ip "data":[ "中國", // 國家 "江蘇", // 省會或直轄市 "南京", // 地區或城市 "電信", // 運營商 "210000", // 郵政編碼 "025" // 地區區號 ] } 

 

2、ip-api,部分數據不准確,但不限請求次數,且可批量json請求,返回數據可選:json、xml、csv
  幫助文檔

  

請求接口                                     請求方式           響應
 http://ip-api.com/json/8.8.8.8?lang=zh-CN    HTTP GET           如下
 
        
 1 {  2 "status":"success",  3 "country":"美國",  4 "countryCode":"US",  5 "region":"VA",  6 "regionName":"弗吉尼亞州",  7 "city":"Ashburn",  8 "zip":"20149",  9 "lat":39.0438, 10 "lon":-77.4874, 11 "timezone":"America/New_York", 12 "isp":"Level 3", 13 "org":"Google LLC", 14 "as":"AS15169 Google LLC", 15 "query":"8.8.8.8"
16 }

 

哈哈,上python,獲取信息

 1 """
 2 簡化版  3 """
 4 import requests  5 lid=input("請輸入你要查詢的IP:")  6 
 7 headers = {  8         'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}  9 url="http://ip-api.com/json/{}?fields=status,message,country,countryCode,region,regionName,city,zip,lat,lon,timezone,isp,org,as,query&lang=zh-CN".format(lid) #加headers反爬
10 rb=requests.get(url,headers=headers) 11 print(rb.text)

 

能獲取ip信息的,但有點亂,不喜歡,個人有點強迫症,優化一下吧

 1 #!/usr/bin/env python3
 2 # coding:utf-8
 3 # 2019/11/14 14:45
 4 #lanxing
 5 import requests  6 import time  7 
 8 strat=time.time()  9 def chax(): 10         lid=input("請輸入你要查詢的IP:") 11         print("**************************************開始獲取IP信息**********************************************") 12         head = { 13         'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'} 14         url="http://ip-api.com/json/{}?fields=status,message,country,countryCode,region,regionName,city,zip,lat,lon,timezone,isp,org,as,query&lang=zh-CN".format(lid) 15         rb=requests.get(url,headers=head) 16         # gf=BeautifulSoup(rb)
17         # print(rb.text)
18         # print("status:"+rb.content["status"])
19         response = rb.text 20         print(type(response)) 21         # print(response)
22         # 把str轉換到dic
23         dict_response = eval(response) 24         # for i in dict_response.items():
25         # print(i)
26 
27         for key,value in dict_response.items(): 28 
29                 print("IP信息:" + str(key) + " : " + str(value)) 30 
31 
32 chax() 33 end=time.time() 34 print("**************************************大佬,查詢IP信息完成!**************************************") 35 print('查詢耗時:',end-strat)

 

 

這個,理論上沒有查詢次數限制,有空再搞搞

 

參考文檔:https://www.jianshu.com/p/bd0616fc30b8

                  https://ip-api.com/docs/api:json#test

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM