python 根据IP获取到国家输出占据百分比

网上找了个接口,直接用python去跑循环拿数据。

 

import requests

country_list = []


def get_country(ip_str):
    data = {
        "ip": str(ip_str)
    }
    url = "https://api.uutool.cn/ip/location_batch/"
    response = requests.post(url, data=data).json()['data']
    for row in response['rows']:
        country_list.append(row['country'])


def country_persentage():
    countrys = set(country_list)
    for country in countrys:
        persentage = '{:.2f}%'.format(int(country_list.count(country)) / len(country_list) * 100)
        print(f"{country} 占据 {persentage}")
    ''''
    统计每个国家出现的次数,
    国家的次数 / 总数 = 百分比

    '''


if __name__ == '__main__':
    with open("/Users/Desktop/ip.txt") as f:
        a = 0
        str1 = ""
        for i in f.readlines():
            a = a + 1
            str1 = str1 + f"{i}"
            if a % 100 == 0:
                get_country(str1.replace("\n", "|"))
                str1 = ""
    country_persentage()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值