What python libraries can tell me approximate location and time zone given an IP address?

后端 未结 13 715
借酒劲吻你
借酒劲吻你 2020-11-28 03:31

Looking to implement better geo-location with Python.

13条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 04:15

    Install the Python module pygeoip

    C:>pip install pygeoip

    Download the binary file of GeoLite City from here: https://dev.maxmind.com/geoip/legacy/geolite/

    Then:

    >>> import pygeoip
    >>> g = pygeoip.GeoIP('GeoLiteCity.dat')
    >>> ip = '134.222.199.110' #just an example
    >>> g.record_by_addr(ip)['latitude']
    52.38239999999999
    >>> g.record_by_addr(ip)['longitude']
    4.899499999999989
    >>> g.record_by_addr(ip)['time_zone']
    'Europe/Amsterdam'

    Contrary to the freegeoip solution I see in the other comments, this option has no limits on the number of IPs per hour, can be used locally without Internet connection, and the geocoordinates are generally more accurate.

提交回复
热议问题