Is there a way to get the computer's physical position using Python, preferably without an API, or with a free API? I've searched around a bit, and the only free API I've found is very, very inaccurate. I only need this to be somewhat accurate, as it's for getting local weather.
-
1. what have you tried, 2. how precise are you looking for? – mfsiega Aug 03 '12 at 01:05
-
@mfrankli I've searched for "IP geolocation API", and IP2location.com is the only thing I found. I live in a major city, and the example on their site for my IP is way off. It is for finding local weather, so ≈50 miles is OK, but the one I found is almost a state off. – tkbx Aug 03 '12 at 01:09
-
you might want to update your question to reflect those things (especially the latter). – mfsiega Aug 03 '12 at 01:10
-
IP-based location detection is not always very good at all. You should check the tools you try when connected through other ISPs or from other locations (friend's house, etc), and also check your reported location across multiple tools from each location. Don't be too quick to blame the tool, basically. – Silas Ray Aug 03 '12 at 01:19
6 Answers
You can try using MaxMind's GeoIP Python API with their free GeoLite City database. Accuracy may vary, more details here.
Also take a look at this question for other alternatives.
- 1
- 1
- 12,347
- 13
- 71
- 93
import urllib2
import json
# Automatically geolocate the connecting IP
f = urllib2.urlopen('http://freegeoip.net/json/')
json_string = f.read()
f.close()
location = json.loads(json_string)
print(location)
location_city = location['city']
location_state = location['region_name']
location_country = location['country_name']
location_zip = location['zipcode']
Send HTTP GET requests to: freegeoip.net/{format}/{ip_or_hostname} to receive a JSON output that Python can parse.
I get the following JSON keys, which should be sufficient for what you are needing:
- ip
- country_code
- country_name
- region_code
- region_name
- city
- zipcode
- latitude
- longitude
- metro_code
- area_code
- 466
- 6
- 10
You can scrape it from a webpage like http://www.geoiptool.com/ for example.
- 1,031
- 2
- 8
- 10
-
Scraping is not ideal, the usage of a web API is significantly better because it is less susceptible to parsing changes. See my answer below. – Jason Parham Oct 01 '14 at 18:45
-
@JasonParham http://freegeoip.net/ is currently not working, Scraping or using a web API are worse than using the `GeoIP` database which you can be download offline and you don't have to worry about these services being down or the time taken to download the data from server – jamylak Jan 08 '15 at 10:16
I rediscovered another weather API which I don't like quite as much (Weather Underground), but can optionally determine the location. Might use this if I can't something like a geoiptool scraper to work.
- 14,366
- 27
- 82
- 118
If you know the public IP of the device then you can do so by using freegeip. Below is the Python 3.4.2 code for getting location and time zone.
>>> import requests
>>> ip = '141.70.111.66'
>>> url = 'http://freegeoip.net/json/'+ip
>>> r = requests.get(url)
>>> js = r.json()
>>> js['country_code']
'DE'
>>> js['country_name']
'Germany'
>>> js['time_zone']
'Europe/Berlin'
>>> js['city']
'Stuttgart'
>>> js.items()
dict_items([('latitude', 48.7667), ('ip', '141.70.111.66'), ('region_code', 'BW'), ('country_code', 'DE'), ('city', 'Stuttgart'), ('zip_code', '70173'), ('country_name', 'Germany'), ('longitude', 9.1833), ('region_name', 'Baden-Württemberg Region'), ('time_zone', 'Europe/Berlin'), ('metro_code', 0)])
- 1,404
- 15
- 17
get in https://python-visualization.github.io/folium/plugins.html find, its veary easy my friend folium.plugins.LocateControl
-
1Seba Ledezma, a link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](/help/deleted-answers) – sta Sep 19 '21 at 04:23
-
As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 19 '21 at 05:12