Here is an example of using an IP to get a location API in Python using the requests library and the ipapi API:

import requests

ip = '8.8.8.8'  # Replace with the IP address you want to locate
api_key = 'your_api_key'  # Replace with your API key from ipapi.com

url = f'https://ipapi.co/{ip}/json/?key={api_key}'

response = requests.get(url)

if response.status_code == 200:
    data = response.json()
    city = data['city']
    country = data['country_name']
    print(f'The IP address {ip} is located in {city}, {country}.')
else:
    print(f'Error: {response.status_code}')

This example sends a request to the ipapi API with the specified IP address and API key. If the request is successful, the response is parsed as JSON and the city and country are extracted from the data. The location information is then printed to the console. If the request is not successful, an error message is printed to the console.

ip to get location api python example

原文地址: https://www.cveoy.top/t/topic/8Xd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录