我正在尝试使用pythongeoip将IP地址转换为Maxmind数据库的地理详细信息。在
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。错误。地址未找到错误:地址103.229.234.197不在数据库中。“
^{pr2}$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
还是不走运。在
有什么建议吗。在
这里的问题是城市数据库调用,而不是在值打印中,因此您可以尝试以下操作:
相关问题 更多 >
编程相关推荐