美文网首页
python批量换IP到国家信息

python批量换IP到国家信息

作者: 国服最坑开发 | 来源:发表于2024-12-11 11:49 被阅读0次
import requests

# 将IP地址分成批量列表
ip_addresses = [
    '8.8.8.8',
    '8.8.4.4'
]


# 定义查询函数
def query_ip_geolocation(ip_list):
    url = "http://ip-api.com/batch"  # 使用ip-api批量查询
    headers = {"Content-Type": "application/json"}
    try:
        response = requests.post(url, json=ip_list, headers=headers)
        if response.status_code == 200:
            return response.json()
        else:
            print(f"Error: {response.status_code} - {response.text}")
            return []
    except Exception as e:
        print(f"Exception occurred: {e}")
        return []


if __name__ == '__main__':

    # 按10个IP分批查询
    batch_size = 10
    for i in range(0, len(ip_addresses), batch_size):
        batch = ip_addresses[i:i + batch_size]
        results = query_ip_geolocation(batch)
        for result in results:
            # print(f"{result['query']}\t{result['country']}, Region: {result.get('regionName', 'N/A')}, City: {result.get('city', 'N/A')}")
            print(f"{result['country']}\t{result['query']}")

相关文章

网友评论

      本文标题:python批量换IP到国家信息

      本文链接:https://www.haomeiwen.com/subject/kswzsjtx.html