2788647047_111py
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")
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步