废话不多数直接上代码
from urllib.parse import quote
from urllib import request
import json
import pandas
import sys
import csv
url = "https://ip.zxinc.org/api.php?type=json&ip="
thefile=open("ip.csv")#原始IP地址文件
count=-1
while True:
buffer=thefile.read()
if not buffer:
break
count+=buffer.count('\n')
with open('ip.csv', 'r') as file:
reader = csv.reader(file)
# 读取并忽略第一行(标题行)
next(reader)
# 读取前两行数据
for row in range(count):
# 读取一行数据
list = next(reader)
for i in list:
req_ur = url +i
data = ''
f=request.urlopen(req_ur)
data = f.read()
result=json.loads(data)
j = open('ip_local.csv','a+') # 生成IP归属地文件
print(i,result['data']['country'],file=j) # 打印到终端