不光要给IP加子网掩码,有的时候还要查IP的归属地,好几千个,不批量一下,很累,还看不看小姐姐了,看,当然要看,那就写吧~
要求:批量的查询xlsx,查询当前IP的福归属地,再输出到xlsx中,
# -*- encoding: utf-8 -*-
'''
@File : ip2area_py3_taobao.py
@Time : 2019/06/25 23:21:04
@Author : JE2Se
@Version : 1.0
@Contact : admin@je2se.com
@WebSite : https://www.je2se.com
'''
import pandas as pd
import urllib.request
import json
import os
import time
# 设置工作目录
os.chdir('/Users/je2se/Desktop/') #自行更换文件的存放目录,建议脚本和目标文件都放在一个地方
df = pd.read_excel('IP.xlsx', sheet_name=0)
df_length = len(df)
# 读取'IP'列数据放入列表
ip_data = df.IP.tolist()
city_data = []
isp_data = []
country_data = []
region_data = []
country_region_city = []
for i in ip_data:
url = ('http://ip.taobao.com/service/getIpInfo.php?ip=%s') % i
# print(url)
urlobject = urllib.request.urlopen(url)
urlcontent = urlobject.read()
res = json.loads(urlcontent)
# print(res)
country = res['data']['country']
country_data.append(country)
region = res['data']['region']
region_data.append(region)
city = res['data']['city']
city_data.append(city)
isp = res['data']['isp']
isp_data.append(isp)
for i in range(len(ip_data)):
# print(country_data[i] + region_data[i] + city_data[i])
area = country_data[i] + region_data[i] + city_data[i]+isp_data[i]
country_region_city.append(area)
# print(country_region_city)
ipinfo = {"IP":ip_data, "城市":country_region_city}
print("ok")
result = pd.DataFrame(ipinfo)
result.to_excel('result.xlsx')
使用了淘宝IP的接口,就不上图了 ,直接说缺点,淘宝IP的这个接口好不稳定啊,动不动就404,不知道是不是我使用网的问题,
这也不行啊,这多看不了隔壁桌位的小姐姐多长时间啊,等它连接等的就蛋疼。换接口~~~~~
# -*- encoding: utf-8 -*-
'''
@File : ip2area_py3_chinaz.py
@Time : 2019/06/25 23:26:58
@Author : JE2Se
@Version : 1.0
@Contact : admin@je2se.com
@WebSite : https://www.je2se.com
'''
import sys
import os
import requests
from bs4 import BeautifulSoup
import tablib
import socket
import re
path = "/Users/je2se/Desktop/" # 存放路径
filename = "ip" # 文件名称
dataset1 = tablib.Dataset() # 数据集合
ip_list = [] # IP列表
# 写XLS
def into_els(new_ip,taglocality):
headers = ('ip', '地区') # 首行字段
dataset1.headers = headers
dataset1.append((new_ip,taglocality))
#匹配出IP地址函数
def matchIP (new_ip):
url = "http://ip.tool.chinaz.com/"
try:
url = url+str(new_ip)
except:
pass
## 根据传入的IP地址截取出地区
wbdata = requests.get(url).text
soup = BeautifulSoup(wbdata, 'lxml')
for tag in soup.find_all('span', class_='Whwtdhalf w50-0'):
tag_extractl = tag.get_text().encode('utf-8')
if tag_extractl.find(b"IP\xe7\x9a\x84\xe7\x89\xa9\xe7\x90\x86\xe4\xbd\x8d\xe7\xbd\xae"): #过滤掉【IP的物理位置】这个字符
print("%s||%s" % (new_ip,tag.get_text())) #输出域名,IP,地区
into_els(new_ip,tag.get_text()) #写数据到数据集合中
#读取文件函数
def read_file(file_path):
if not os.path.exists(file_path):
sys.exit(0)
else:
with open(file_path, 'r') as source:
for line in source:
ip_list.append(line.rstrip('\r\n').rstrip('\n'))
for ip in ip_list:
matchIP(ip)
hFile = open(path + filename + '.xls', "wb")
hFile.write(dataset1.xls)
hFile.close()
if __name__ == '__main__':
file_str="/Users/je2se/Desktop/ip.txt"
read_file(file_str)
这次换成了chinaz的接口,这回的接口还能识别网段等等,比上面的好用多了。

<hr>
里面有的代码我是直接用的网上的,做了部分修改,为啥用网上的呢?
因为,我写也是这么写,也多不出什么花来,就直接复制了,多出点时间,看看隔壁的小姐姐。