【Python】批量查询-提取站长之家IP批量查询的结果v1.0

时间:2023-03-09 03:21:15
【Python】批量查询-提取站长之家IP批量查询的结果v1.0

0 前言

写报告的时候为了细致性,要把IP地址对应的地区给整理出来。500多条IP地址找出对应地区复制粘贴到报告里整了一个上午。
为了下次更好的完成这项重复性很高的工作,所以写了这个小的脚本。

1 使用库

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文件路径

【Python】批量查询-提取站长之家IP批量查询的结果v1.0

复制到Notepad++,然后粘贴到Word中。爽爽爽。。。

【Python】批量查询-提取站长之家IP批量查询的结果v1.0