Python GeoIP2 AddressNotFoundError

卡尔提克

我正在尝试使用Python GeoIP使用Maxmind数据库将IP地址转换为Geo详细信息。

import urllib2
import csv
import geoip2.database
db = geoip2.database.Reader("GeoLite2-City.mmdb")
target_url="http://myip/all.txt"
data = urllib2.urlopen(target_url)
for line in data:
        response = db.city(line.strip())
        print line.strip(), response.country.name, response.country.iso_code, response.location.longitude, response.location.latitude

我收到错误消息“ geoip2.errors.AddressNotFoundError:地址103.229.234.197不在数据库中。”

    Traceback (most recent call last):
  File "script.py", line 14, in <module>
    response = db.city(line.strip())
  File "/usr/lib/python2.7/site-packages/geoip2/database.py", line 110, in city
    return self._model_for(geoip2.models.City, 'City', ip_address)
  File "/usr/lib/python2.7/site-packages/geoip2/database.py", line 180, in _model_for
    record = self._get(types, ip_address)
  File "/usr/lib/python2.7/site-packages/geoip2/database.py", line 176, in _get
    "The address %s is not in the database." % ip_address)
geoip2.errors.AddressNotFoundError: The address 103.229.234.197 is not in the database.

Maxmind db提到的地址不在数据库中。但是它不会伤害我,但是如何忽略此错误并获得我可用的输出呢?

尝试除任何错误(尽管这不是最佳方法)之外,还希望获得特定的AddressNotFoundError。

try:
     print line.strip(), response.country.name, response.country.iso_code, response.location.longitude, response.location.latitude
except:
       pass

还,

try:
     print line.strip(), response.country.name, response.country.iso_code, response.location.longitude, response.location.latitude

except AddressNotFoundError:
     pass

仍然没有运气。

有什么建议么。

伊库肯

这里的问题是db.city调用中发生的异常,而不是值打印中发生的异常,因此您可以尝试以下操作:

import urllib2
import csv
import geoip2.database
db = geoip2.database.Reader("GeoLite2-City.mmdb")
target_url="http://myip/all.txt"
data = urllib2.urlopen(target_url)
for line in data:
    try:
        response = db.city(line.strip())
        print line.strip(), response.country.name, response.country.iso_code, response.location.longitude,   response.location.latitude
    exception:
        pass

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Geoip2的python库在pySpark的map函数中不起作用

来自分类Dev

Maxmind GeoIP2教程(操作方法)?

来自分类Dev

MaxMind的GeoIP2 Java API线程安全吗?

来自分类Dev

使用maxmind geoip2调出城市名称

来自分类Dev

MaxMind GeoIP2 IPv6没有结果?

来自分类Dev

在Django中为GeoIP2创建项目设置

来自分类Dev

使用maxmind geoip2呼出城市名称

来自分类Dev

MaxMind GeoIP2 IPv6没有结果?

来自分类Dev

如何为maxmind geoip2 eclipse添加依赖项

来自分类Dev

Maxmind GeoIP2教程(操作方法)?

来自分类Dev

MaxMind:将GeoIP2 mmdb加载到内存中以快速读取

来自分类Dev

MaxMind MMDB二进制GeoIP2数据库的大小是多少?

来自分类Dev

使用GeoIp2进行PHP重定向仅一次

来自分类Dev

如何正确处理来自Rust中maxminddb :: geoip2的AddressNotFound错误?

来自分类Dev

GeoIP2 MaxMind pom和我本地的pom有什么区别?

来自分类Dev

MaxMind MMDB二进制GeoIP2数据库的大小是多少?

来自分类Dev

python-geoip在python3.4上不起作用吗?

来自分类Dev

Python 2与Python 3导入

来自分类Dev

Python FIFO 2维

来自分类Dev

Python OSError:[Errno 2]

来自分类Dev

消除Python挑战2

来自分类Dev

Python 2 Layer Scoreboard?

来自分类Dev

Python FIFO 2维

来自分类Dev

消除Python挑战2

来自分类Dev

在Python 2中并发

来自分类Dev

Python2:'!../'的含义

来自分类Dev

从 Mac 卸载 Python 2

来自分类Dev

与Python 2同时安装并运行Python 3

来自分类Dev

与Python 2同时安装并运行Python 3