【问题标题】:vba scrape non-static web table datavba 抓取非静态 web 表数据
【发布时间】:2016-08-12 10:09:11
【问题描述】:

我正在尝试创建一个从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()

【问题讨论】:

  • end sub() 不正确
  • 抱歉,打错了。

标签: vba excel internet-explorer web-scraping


【解决方案1】:

您必须从表格对象中为每一行选择“tr” 使用 IE 时,应返回非 statix(执行 javascript)

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

用 debug.print 测试

你选择html然后tables -> 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

【讨论】:

    猜你喜欢
    • 2021-07-18
    • 2021-08-08
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    相关资源
    最近更新 更多