import socket
import csv
# 读取文件中的域名列表
with open("111.txt", "r") as f:
domains = [line.strip() for line in f.readlines() if line.strip()]
# 查询域名对应的 IP 地址
ip_addresses = {}
for domain in domains:
try:
ip = socket.gethostbyname(domain)
ip_addresses[domain] = ip
except:
ip_addresses[domain] = "无法解析"
# 将结果输出为 CSV 文件
with open("ip_addresses.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(["域名", "IP 地址"])
for domain, ip in ip_addresses.items():
writer.writerow([domain, ip])
print("IP 地址查询结果已保存到 ip_addresses.csv 文件中。")
标签:domain,writer,addresses,ip,py,2788647047,IP,csv From: https://www.cnblogs.com/JLPeng/p/18362745