How to detect the country and city of a user accessing your site?

后端 未结 3 1079
长发绾君心
长发绾君心 2021-02-04 17:48

How can you detect the country of origin of the users accessing your site?

I\'m using Google Analytics on my site and can see that I have users coming from different re

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-04 18:24

    Max mind costs money. For a free yet country code only solution you can try: https://pypi.python.org/pypi/geoip2nation/

    pip install geoip2nation

    from geoip import geoip
    r = geoip.GeoIp()
    r.load_memory()
    r.resolve("12.12.12.12").country_code
    #This prints : 'US'
    
    print r.resolve("123.44.57.4")
    #This prints : {'country': 'Korea (South)', 'host_name': '', 'country_code': 'KR'}
    
    r.resolve2("12.12.12.12")
    #This prints : 'US'
    

提交回复
热议问题