python编写工具番外篇(6)-- 处理IPIP的查询信息

一、需求

在ipip查询一个IP的信息,但是仅仅想知道这个IP是不是IDC或者基站的信息。如下图,

ipip貌似也没有提供相对于的代码,直接burp抓包,看看没签名就可以整一个小脚本。	

主要涉及到Python的返回数据解析问题。

在这里插入图片描述

二、实现代码

import requests
import time
from bs4 import BeautifulSoup


def get_ipinfo(ip):
    #print(ip)
    url = "https://www.ipip.net/ipquery.html"
    headers = {
        'Cookie': 'xxx', 'Content-Length': '62'
        , 'Cache-Control': 'max-age=0'
        , 'Sec-Ch-Ua': '";Not A Brand";v="99", "Chromium";v="94"'
        , 'Sec-Ch-Ua-Mobile': '?0'
        , 'Sec-Ch-Ua-Platform': '"macOS"'
        , 'Upgrade-Insecure-Requests': '1'
        , 'Origin': 'https://www.ipip.net'
        , 'Content-Type': 'application/x-www-form-urlencoded'
        ,
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
        ,
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
        , 'Sec-Fetch-Site': 'same-origin'
        , 'Sec-Fetch-Mode': 'navigate'
        , 'Sec-Fetch-User': '?1'
        , 'Sec-Fetch-Dest': 'document'
        , 'Referer': 'https://www.ipip.net/ip.html'
        , 'Accept-Encoding': 'gzip, deflate'
        , 'Accept-Language': 'zh-CN,zh;q=0.9'
        , 'Connection': 'close'
    }
    data = "csrf_token=抓包获得&ip="+ip
   # print(data)
    res = requests.post(url=url,headers=headers,data=data)
    res = (res.text)
    #print(res)

#下边是提取是否为IDC,也是重点

    # step01: 将文本内容实例化出bs对象
    soup_obj = BeautifulSoup(res, "lxml")
    # print(soup_obj)

    # step02: 查找标签
    main = soup_obj.find("dl", attrs={"class": "row"})
    # print(main)

    # step03: main标签中的每个dd与dt标签都是所需的内容
    dd = main.find_all("dd")
    # dt = main.find_all("dt")

    # print(dd[9])
    for IDC in dd[10]:
        #print(IDC)
        if IDC == '否':
            continue
        else:
            print("这是IDC")
            print(ip)

    for JZ in dd[11]:
        #print(JZ)
        if JZ == '否':
            continue
        else:
            print("这是基站")
            print(ip)


if __name__ == '__main__':

    # 从同目录下的ip.txt文件内获得文件
    with open('ip.txt', 'r') as f:
        for ip in f:
            ip = ip.replace("\n", "")
            #print(ip)
            try:
                get_ipinfo(ip)
            except:
                print("cookie过期")
            time.sleep(1)

    #res = get_ipinfo('8.8.8.8')

ip.txt的内容如下:

112.203.xx.xx
112.208.xx.xx
112.211.xx.xx
2402:9d80:xxx:xxxx:xxxx:xxxx:xxxx:xxxx
124.66.xx.xx

三、运行结果

假设ip.txt文件内,存在IDC或者基站就会输出:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

划水的小白白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值