import requests
from openpyxl import Workbook
# 创建一个工作簿
wb = Workbook()
# 添加一个工作表
ws = wb.active
# 读取文件中的域名
with open("domains.txt", "r") as f:
domains = f.readlines()
# 遍历域名,获取对应的IP地址
for domain in domains:
# 去除注释和空格
domain = domain.strip().split("#")[0].strip()
# 检查是否为域名
if domain:
try:
# 请求ip.cn获取IP地址
response = requests.get(f"https://ip.cn/index.php?ip={domain}&language=cn", timeout=10)
# 解析网页内容
html = response.text
# 提取IP地址
ip_address = ""
if "IP:" in html:
start_index = html.index("IP:") + 3
end_index = html.index(" ", start_index)
ip_address = html[start_index:end_index]
# 将域名和IP地址写入工作表
ws.append([domain, ip_address])
except requests.exceptions.ConnectionError:
ws.append([domain, "Error: ConnectionError"])
except requests.exceptions.Timeout:
ws.append([domain, "Error: Timeout"])
except requests.exceptions.RequestException as e:
ws.append([domain, f"Error: {str(e)}"])
# 保存工作簿为ods文件
wb.save("domains_ip.ods")