python查詢ip歸屬地

2021-09-05 03:55:12 字數 1550 閱讀 4039

本來想呼叫阿里的ip介面查詢ip歸屬地。結果發現阿里的介面非常不給力,主要是不準確,不過是免費的且有地區和isp的資訊。以下是實現**

# -*- coding: utf-8 -*-

import requests

def checkip(ip):

url = ''

try:

r = requests.get(url, params=ip, timeout=3)

except requests.requestexception as e:

print(e)

else:

json_data = r.json()

if json_data[u'code'] == 0:

print '所在國家: ' + json_data[u'data'][u'country'].encode('utf-8')

print '所在地區: ' + json_data[u'data'][u'area'].encode('utf-8')

print '所在省份: ' + json_data[u'data'][u'region'].encode('utf-8')

print '所在城市: ' + json_data[u'data'][u'city'].encode('utf-8')

print '所屬運營商:' + json_data[u'data'][u'isp'].encode('utf-8')

else:

print '查詢失敗,請稍後再試!'

但是多次查詢發現ip歸屬地不準確,於是使用17mon的ip查詢介面。但是17mon分付費和免費的庫介面,我用的免費的測試,介面返回的字段有限,只有國家、省份、城市。**如下

# -*- coding: utf-8 -*-

import requests

def lookup(ip):

url = '' + ip

try:

r = requests.get(url, timeout=3)

except requests.requestexception as e:

print(e)

json_data = r.json()

print  '所在國家:' + json_data[0].encode('utf-8')

print  '所在省份:' + json_data[1].encode('utf-8')

print  '所在城市:' + json_data[2].encode('utf-8')

測試也不錯,公司要使用還是選擇購買付費的庫查詢介面吧。

IP歸屬地查詢

urllib http api請求 re ip格式驗證 返回結果提取 tkinter 介面布局 樣式 ip定位,根據盛大ip查詢api所編寫 anthor yanzhenwei yanzhenwei1101 qq.com create version v1.0beta01 2012 03 02 la...

查詢ip歸屬地 小應用

快過年了,寫個小玩意 下面這個應用涉及到了一些基礎東西.1 http api呼叫 2 asynctask使用 3 textview 自動匹配 4 string 的spilt的正規表示式 應用截圖 截圖傳不上來.貼下邊 public class mainactivity extends activit...

python 號碼歸屬地查詢

號碼歸屬地查詢 def sim number s 5582 1860101 010 北京市 北京聯通gsm卡 5583 1860100 010 北京市 北京聯通gsm卡 5584 1368141 010 北京市 北京移動神州行卡 5585 1860111 010 北京市 北京聯通gsm卡 5586 ...