0 前言
写报告的时候为了细致性,要把IP地址对应的地区给整理出来。500多条IP地址找出对应地区复制粘贴到报告里整了一个上午。
为了下次更好的完成这项重复性很高的工作,所以写了这个小的脚本。
1 使用库
- 1)requests
- 简介:Requests是一常用的http请求库,它使用python语言编写,可以方便地发送http请求,以及方便地处理响应结果。
- 安装方法:pip install requests
- 2)BeautifulSoup
- 简介:Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档
- 安装方法:pip install beautifulsoup4
2 代码
#-*-coding:utf-8-*-
# chinaz-extractIPandCountry.py
# 主要功能:批量查询-提取站长之家IP批量查询的结果
# By zzzhhh (http://www.cnblogs.com/17bdw)
import sys
import os
import requests
from bs4 import BeautifulSoup
ip_list = []
#匹配出IP地址函数
def matchIP (str):
url = "http://ip.chinaz.com/"
url = url+str
## 根据传入的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("IP的物理位置"): #过滤掉【IP的物理位置】这个字符
print str, tag.get_text()
#读取文件函数
def read_file(file_path):
if not os.path.exists(file_path):
print 'Please confirm correct filepath !'
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)
if __name__ == '__main__':
file_str=raw_input('Input file IP.txt filepath eg:D:\\\\test.txt \n')
read_file(file_str) #读取文件
3 效果
输入存有IP的.txt文件路径
复制到Notepad++,然后粘贴到Word中。爽爽爽。。。