Python IP to Location API Example - Get City, Region, and Country
Here is an example of using the IP to get location API in Python:
import requests
ip_address = '8.8.8.8'
url = f'https://ipapi.co/{ip_address}/json/'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
print(f'City: {data['city']}')
print(f'Region: {data['region']}')
print(f'Country: {data['country_name']}')
else:
print('Failed to get location information.')
In this example, we are using the IP to get location API provided by ipapi.co. We pass the IP address we want to lookup in the URL and make a GET request to the API. If the request is successful, we retrieve the JSON response and print out the city, region, and country information. If the request fails, we print an error message.
原文地址: https://www.cveoy.top/t/topic/myiP 著作权归作者所有。请勿转载和采集!