AI给出的答案
import requests
import pandas as pd
from openpyxl import Workbook, load_workbook
from time import sleep
from tqdm import tqdm
df = pd.read_excel(r"Y:\raw\2024\2024067\攻击流量大于10Gbps.xlsx")
ips = df["IP"].tolist()
data = {}
head = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"}
wb = Workbook()
ws = wb.active
row_num = 1
progress_bar = tqdm(total=len(ips), desc="Querying IP", unit="IP")
for i, ip in enumerate(ips):
try:
response = requests.get('http://ip.zxinc.org/api.php?type=json&ip=' + ip, headers=head, timeout=10)
text = response.json()
location = text["data"]["location"]
d = {ip: location}
data.update(d)
ws.cell(row=row_num, column=1, value=ip)
ws.cell(row=row_num, column=2, value=location)
row_num += 1
progress_bar.update(1)
if (i + 1) % 200 == 0: # 查询200个IP后休息5分钟
progress_bar.set_description("Resting")
progress_bar.refresh()
sleep(60) # 休息5分钟
except requests.exceptions.Timeout:
print(f"Timeout occurred while querying IP: {ip}. Retrying...")
sleep(5) # 等待一段时间后重新尝试
i -= 1 # 重新查询当前IP
progress_bar.close()
wb.save('ips.xlsx')
自己写出的代码:
import requests
import pandas as pd
from openpyxl import Workbook,load_workbook
df = pd.read_excel(r"C:\Users\Administrator\Desktop\ip.xlsx")
ips = df["login_ip"].tolist()
data = {}
head = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"}
for ip in ips :
response = requests.get('http://ip.zxinc.org/api.php?type=json&ip='+ip , headers = head)
text = response.json()
location = text["data"]["location"]
d = {ip:location}
data.update(d)
wb = Workbook()
ws = wb.active
row_num = 1
for ip,location in data.items() :
ws.cell(row = row_num,column = 1, value = ip)
ws.cell(row=row_num, column = 2, value=location)
row_num += 1
wb.save('ips.xlsx')