python免费api查询ip地址的中文最详细的地理位置不用apikey中文!
可以使用淘宝IP地址库提供的API来查询IP地址的中文地理位置,无需API key。
具体步骤如下:
- 构造查询URL,将需要查询的IP地址替换到URL中:
url = 'http://ip.taobao.com/service/getIpInfo.php?ip={}'.format(ip_address)
- 发送HTTP请求,并获取响应数据:
import requests
response = requests.get(url)
data = response.json()
- 解析响应数据,获取中文地理位置数据:
if data['code'] == 0:
country = data['data']['country']
region = data['data']['region']
city = data['data']['city']
isp = data['data']['isp']
location = f"{country} {region} {city} {isp}"
print(location)
else:
print('查询失败')
完整代码示例:
import requests
ip_address = '123.56.67.89'
url = 'http://ip.taobao.com/service/getIpInfo.php?ip={}'.format(ip_address)
response = requests.get(url)
data = response.json()
if data['code'] == 0:
country = data['data']['country']
region = data['data']['region']
city = data['data']['city']
isp = data['data']['isp']
location = f"{country} {region} {city} {isp}"
print(location)
else:
print('查询失败')
``
原文地址: https://www.cveoy.top/t/topic/fsrW 著作权归作者所有。请勿转载和采集!