Ip Location
TNFSH Python教材
註:geocoder 套件已停止維護,且其查詢端點現在多半需要申請 token 才能使用,現行做法改用 requests 直接呼叫免費的 ip-api.com 服務。
1: import requests 2: 3: data = requests.get('http://ip-api.com/json/8.8.8.8').json() 4: print(data) 5: print('國家:', data['country']) 6: print('城市:', data['city'])
{'status': 'success', 'country': 'United States', 'countryCode': 'US', 'region': 'VA', 'regionName': 'Virginia', 'city': 'Ashburn', 'zip': '20149', 'lat': 39.03, 'lon': -77.5, 'timezone': 'America/New_York', 'isp': 'Google LLC', 'org': 'Google Public DNS', 'as': 'AS15169 Google LLC', 'query': '8.8.8.8'}
國家: United States
城市: Ashburn