VBA访问网页获取数据

本文主要介绍VBA访问网页获取数据。

实现效果:将网页中表的数据输出到excel中。




VBA代码:

Sub getResource()
    Columns("A:E").ClearContents
    Set HTML = CreateObject("htmlfile")
    With CreateObject("msxml2.xmlhttp")
        URL = "http://data.eastmoney.com/stock/tradedetail.html"
        .Open "get", URL, False
        .send
        HTML.body.innerhtml = StrConv(.responsebody, vbUnicode)
        Cells(1, 2) = HTML.body.document.getElementById("notice_Ddl").DefaultValue
        Set tr = HTML.all.tags("tr")
        j = 1
        For r = 0 To tr.Length - 1
            If Val(tr(r).Cells(0).innertext) + 1 = j Then
                j = j + 1
                Cells(j, 1) = tr(r).Cells(0).innertext
                Cells(j, 2) = tr(r).Cells(1).innertext
                Cells(j, 3) = tr(r).Cells(2).innertext
                Cells(j, 4) = tr(r).Cells(6).innertext
                Cells(j, 5) = tr(r).Cells(8).innertext
            End If
        Next
    End With
End Sub


  • 14
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值