vba刮取非静态Web表数据

时间:2016-08-12 10:09:12

标签: excel vba excel-vba internet-explorer web-scraping

我正在尝试创建一个从web page抓取并导入表格的宏, 更具体地说,我想得到两个表tables pointed by arrows, please ignore the text in the table if it doesn't make sense, I translated using google。这些表是自动更新的,所以我使用IE方法(由@ron),没有刮掉任何数据。我很累,有人可以帮助我吗?我是一个vba新手,感谢任何帮助。

    Sub test()
    ' open IE, navigate to the website of interest and loop until fully loaded
      Set IE = CreateObject("InternetExplorer.Application")
      my_url = "http://www.neeq.com.cn/static/statisticdata.html"

    With IE
       .Visible = False
       .navigate my_url
       .Top = 50
       .Left = 530
       .Height = 400
       .Width = 400

    Do Until Not IE.busy And IE.ReadyState = 4
       DoEvents
    Loop

   End With

  Set tbl = IE.Document.getElementsByTagName("table")
    For Each itm In tbl
         i = 1
        For Each itm2 In itm.Rows
            For Each cell In itm2.Cells
              ActiveSheet.Cells(i, 2) = cell.innerText
              i = i + 1
            Next
        Next
   Next
  end sub()

1 个答案:

答案 0 :(得分:0)

你必须选择" tr"来自您的表对象For Each Rows 当你使用IE时,非statix应该返回(执行javascript)

i = 1
For Each itm2 In  tbl.getElementsByTagName("tr")

使用debug.print进行测试

你选择html然后选择表格 - > tr - > TD

Sub test()
    ' open IE, navigate to the website of interest and loop until fully loaded
      Set IE = CreateObject("InternetExplorer.Application")
      my_url = "http://www.neeq.com.cn/static/statisticdata.html"

    With IE
       .Visible = False
       .navigate my_url
       .Top = 50
       .Left = 530
       .Height = 400
       .Width = 400

    Do Until Not IE.busy And IE.ReadyState = 4
       DoEvents
    Loop

   End With

  Set tbl = IE.Document.getElementsByTagName("table")
    For Each itm In tbl
         i = 1
        For Each itm2 In itm.getElementsByTagName("tr")
            For Each cell In itm2.getElementsByTagName("td")
              ActiveSheet.Cells(i, 2) = cell.innerText
              i = i + 1
            Next
        Next
   Next
  end sub