吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1467|回复: 31
收起左侧

[其他原创] Excel vba 批量 ip地址查询

[复制链接]
aichiyu 发表于 2024-5-6 01:00
本帖最后由 aichiyu 于 2024-5-6 01:02 编辑

需要JSON

下载地址:https://github.com/VBA-tools/VBA-JSON


打开VBA编辑器,选择导入,选择下好的文件JsonConverter.bas导入

Sub GetIPAddressDetails()
    Dim rng As Range
    Dim cell As Range
    Dim xmlhttp As Object
    Dim json As Object
    Dim address As String
    Dim ip As String

    ' 设置要处理的IP地址列范围
    Set rng = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)

    ' 创建XMLHttpRequest对象
    Set xmlhttp = CreateObject("MSXML2.XMLHTTP")

    ' 循环处理每个单元格
    For Each cell In rng
        ' 获取IP地址
        ip = cell.Value

        ' 发送GET请求获取JSON数据
        xmlhttp.Open "GET", "https://searchplugin.csdn.net/api/v1/ip/get?ip=" & ip, False
        xmlhttp.send

        ' 检查请求是否成功
        If xmlhttp.Status = 200 Then
            ' 解析JSON数据
            On Error Resume Next ' 避免在解析失败时抛出错误
            Set json = JsonConverter.ParseJson(xmlhttp.responseText)
            On Error GoTo 0 ' 恢复错误处理

            If Not json Is Nothing Then
                ' 检查是否包含数据节点
                If json("data") Is Nothing Then
                    MsgBox "JSON数据中缺少数据节点!", vbExclamation
                Else
                    ' 获取地址信息
                    address = json("data")("address")
                    ' 将地址信息写入B列
                    cell.Offset(0, 1).Value = address
                End If
            Else
                MsgBox "JSON数据解析失败!", vbExclamation
            End If
        Else
            MsgBox "请求失败:" & xmlhttp.Status & " - " & xmlhttp.statusText, vbExclamation
        End If

        ' 等待1秒
        Application.Wait (Now + TimeValue("0:00:01"))
    Next cell

    ' 释放对象
    Set xmlhttp = Nothing
    Set json = Nothing
End Sub

免费评分

参与人数 2吾爱币 +2 热心值 +2 收起 理由
Marken888 + 1 + 1 我很赞同!
为之奈何? + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

diguoemo 发表于 2024-5-6 12:46
VBA 纯新手,想请问一下楼主,
是把下面的代码直接插入到JsonConverter模块中还是单独添加插入一个新的模块然后在复制代码进去?
shuifanjishi 发表于 2024-5-7 08:40
Set json = JsonConverter.ParseJson(xmlhttp.responseText),这里错误:“JSON数据解析失败!”,是不是要引用什么。
lengbingling 发表于 2024-5-6 05:51
Gxd1703 发表于 2024-5-6 06:27
谢楼主分享!
ltgb 发表于 2024-5-6 06:31
Excel还能干这种事
龍謹 发表于 2024-5-6 06:56
学习了,虽然一般不会用VBA。
chz123 发表于 2024-5-6 07:10
谢谢分享!
52soft 发表于 2024-5-6 07:42
这个工具不错
pdanny 发表于 2024-5-6 07:44
谢谢!努力成为excel的高手
wan456 发表于 2024-5-6 08:09
从某sdn上获取api
xueyuan124 发表于 2024-5-6 08:16
谢谢楼主分享,
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-26 22:05

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表