python3.x利用淘宝的ip地址查询接口批量查询地理位置

直接上代码

#!/usr/bin/python
#coding:utf-8
 
import urllib.request
import json
 
url = 'http://ip.taobao.com/service/getIpInfo.php?ip='
srcFile = 'ip.txt';
outFile = 'result.txt';

def checkTaobaoIP(ip):
    try:
        re = urllib.request.urlopen(url + ip, timeout=1000).read().decode('utf-8')
        data = json.loads(re);
        return "%15s: %s-%s-%s-%s" % (ip,data['data']['country'],data['data']['isp'],data['data']['region'],data['data']['city'])
    except:
    	return "%15s: timeout" % ip
 
if __name__ == "__main__":
 	with open(srcFile, 'r', buffering=163840, encoding='utf8') as srcData:
 		with open(outFile,'w',buffering=163840, encoding='utf8') as outData:
	 		line = srcData.readline();
	 		while line:
	 			outData.write(checkTaobaoIP(line.strip())+'\n');
	 			line = srcData.readline();
	 		else:
	 			print("Done!");

ip.txt内的示例格式:

116.216.30.3
116.216.30.2
61.164.212.246
106.39.1.31
124.205.63.221
183.195.254.178
36.5.117.190
113.161.29.81
113.89.30.181

感谢看完指鹤文章,希望指鹤的文章对您有所帮助。

闲暇时,指鹤喜欢写一些文章,部分发表在了豆瓣,若是您对此感兴趣,您可以点击下面连接支持下指鹤,指鹤在此表示感谢了

绝命笔记 一封匿名信引发的追寻 迷案追凶 量子危机

Leave a Reply