[python]批量获取网址IP与归属地

import requests
import socket
import codecs
import csv

def get_ip(url):
    try:
        h_name = socket.gethostbyname(url)
        IP_addres = socket.gethostbyname(h_name)
    except:
        return "0.0.0.0"
    return IP_addres

def get_location(url):
    ip_address = get_ip(url)
    response = requests.get(f'https://ipapi.co/{ip_address}/json/').json()
    if "error" in response:
        return {"country":"unknow"}

    location_data = {
        "ip": ip_address,
        "city": response.get("city"),
        "region": response.get("region"),
        "country": response.get("country_name")
    }
    return location_data

with codecs.open('1.csv', encoding='utf-8-sig') as f:
    for row in csv.DictReader(f, skipinitialspace=True):
        url = row["link"]
        print(url,end="\t")
        print(get_location(url)["country"])

给TA充电
共{{data.count}}人
人已充电
编程

[JS]如何下载图片到本地

2022-7-6 3:00:23

编程

[BAT]推荐网站与教程

2022-7-18 18:20:26

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
今日签到
搜索