Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 10311|回复: 45

紧急~~VB抓取网页数据

[复制链接]
发表于 2016-8-5 17:52 | 显示全部楼层 |阅读模式
我在别人写好的代码里面改为我想要的地址。为什么总在Set tb = HTML.ALL.tags("table")(0).Rows 出错。 请哪位高人指点一下。

另外我有一个网站登陆后被网站被加密了,是否有办法抓取上面的信息。也请高手指点一下。谢谢



  • Sub test()
  •     Dim HTML, tb, i&, j&, x&, n&, s$
  •     [a2:d9999] = ""
  •     Set HTML = CreateObject("htmlfile")
  •     With CreateObject("msxml2.xmlhttp")
  •         For x = 0 To 63
  •             .Open "get", "网址& 30 * x, False
  •             .Send
  •             Application.Wait (Now + TimeValue("00:00:02"))
  •             HTML.body.innerhtml = .responsetext
  •             Set tb = HTML.ALL.tags("table")(0).Rows
  •             For i = 1 To tb.Length - 1
  •                 n = n + 1
  •                 For j = 0 To tb(i).Cells.Length - 1
  •                     Cells(n + 1, j + 1) = tb(i).Cells(j).innertext
  •                 Next
  •             Next
  •         Next
  •     End With
  •     MsgBox "获取完毕!"
  • End Sub
 楼主| 发表于 2016-8-5 18:07 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2016-8-6 12:11 | 显示全部楼层

我要抓取这个网页http:(增加双//)fz.people.com.cn/skygb/sk/index.php/Index/seach?&p=1
的数据,请问该如何修改下面的代码? ~~~紧急请大神赐教~~~

Option Explicit
Sub test()
    Dim HTML, tb, i&, j&, x&, n&, s$
    [a2:d9999] = ""
    Set HTML = CreateObject("htmlfile")
    With CreateObject("msxml2.xmlhttp")
        For x = 0 To 63
            .Open "get", "http:(增加双//)fz.people.com.cn/skygb/sk/index.php/Index/seach?&p=1
& 20 * x, False
            .Send
            Application.Wait (Now + TimeValue("00:00:02"))
            HTML.body.innerhtml = .responsetext
            Set tb = HTML.ALL.tags("table")(0).Rows
            For i = 1 To tb.Length - 1
                n = n + 1
                For j = 0 To tb(i).Cells.Length - 1
                    Cells(n + 1, j + 1) = tb(i).Cells(j).innertext
                Next
            Next
        Next
    End With
    MsgBox "??è?íê±?!"
End Sub

    MsgBox "获取完毕!"
End Sub

回复

使用道具 举报

发表于 2016-8-6 13:08 | 显示全部楼层
Set tb = HTML.ALL.tags("table")(2).Rows
回复

使用道具 举报

 楼主| 发表于 2016-8-6 13:29 | 显示全部楼层
不行啊!~~
回复

使用道具 举报

发表于 2016-8-6 13:47 | 显示全部楼层
  1. Sub test()
  2.     Cells.Clear
  3.     Set HTML = CreateObject("htmlfile")
  4.     With CreateObject("msxml2.xmlhttp")
  5.         For x = 1 To 6
  6.             .Open "get", "http://fz.people.com.cn/skygb/sk/index.php/Index/seach?&p=" & x, False
  7.             .Send
  8.             HTML.body.innerhtml = .responsetext
  9.             Set tb = HTML.ALL.tags("table")(2).Rows
  10.             For i = 1 To tb.Length - 1
  11.                 n = n + 1
  12.                 For j = 0 To tb(i).Cells.Length - 1
  13.                     Cells(n + 1, j + 1) = tb(i).Cells(j).innertext
  14.                 Next
  15.             Next
  16.         Next
  17.     End With
  18. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2016-8-6 13:50 | 显示全部楼层
你发的代码我试了~~会停止在sned出现黄色!
回复

使用道具 举报

发表于 2016-8-6 15:01 | 显示全部楼层
1229775691 发表于 2016-8-6 13:50
你发的代码我试了~~会停止在sned出现黄色!

我的可以
回复

使用道具 举报

 楼主| 发表于 2016-8-6 15:07 | 显示全部楼层
请教一下这个为什么要改成 0变成2

请帮助我理解这两句话!非常感谢!
HTML.body.innerhtml = .responsetext
Set tb = HTML.ALL.tags("table")(2).Rows


回复

使用道具 举报

 楼主| 发表于 2016-8-6 18:22 | 显示全部楼层
在我的电脑上还是不能运行时为什么???高手指点一下!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-27 23:32 , Processed in 0.329961 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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