Python IP Details API: Fetch Location and More
To get IP details through an API in Python, you can use the 'requests' library to make HTTP requests to the API endpoint. Here is an example code snippet that uses the 'ipdata.co' API to get the details of a given IP address:
import requests
ip_address = '8.8.8.8' # replace with the IP address you want to get details for
api_key = 'your_api_key_here' # replace with your API key
url = f"https://api.ipdata.co/{ip_address}?api-key={api_key}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
print(f'IP: {data['ip']}')
print(f'Country: {data['country_name']} ({data['country_code']})')
print(f'City: {data['city']}')
print(f'Latitude: {data['latitude']}')
print(f'Longitude: {data['longitude']}')
else:
print('Error:', response.status_code)
Note that you need to sign up for an API key from 'ipdata.co' before you can use this API. Replace 'your_api_key_here' with your actual API key.
原文地址: https://www.cveoy.top/t/topic/mz1v 著作权归作者所有。请勿转载和采集!