使用宏从网页获取tabledata到Excel

时间:2013-08-10 05:25:52

标签: excel excel-vba web-scraping vba

我使用Excel工作表进行一些计算,例如incometax回报的评估。我需要将网站上的数据提取到Excel表格中。我通过逐步使用VBA成功地完成了它

  1. 在VBA中创建Internet Explorer应用程序
  2. 导航至网站网址并登录
  3. 使用Excel表单中已有的唯一ID自动填写表单
  4. 现在提交表单和结果页面包含表格形式的数据
  5. 现在使用getelementsbyid("tableid")我在Excel工作表中复制和粘贴数据。
  6. 我的问题

    1. 所有表格都没有ID或名称
    2. 有很多桌子
    3. 现在我想从没有id的表中提取数据,这是从顶部开始的第三个表。这该怎么做?我努力了。我不想要所有表,因为这些表中的行总是改变所以当我复制所有表数据时。

1 个答案:

答案 0 :(得分:0)

开始录制宏,转到数据 - >在Web上,在导入对话框中打开所需的网页,选择所需的表,根据需要设置所有导入选项,然后导入数据。然后停止录音。

您将获得一个带有自动生成的宏代码的锅炉板,然后根据您的需要精确调整它将是微不足道的。

我做了很多次,这很容易,我现在没有存储剪辑与你分享。

更新:这是如何从此页面导入您的问题

Sub Macro1()
'
' Macro1 Macro
'

'
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://stackoverflow.com/questions/18158928/get-tabledata-from-webpage-into-excel-using-macro/18160278#18160278" _
        , Destination:=Range("$A$1"))
        .Name = "18160278#18160278"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlOverwriteCells ' adjust this setting to your needs
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = "1" ' this is the number of the required table on a page
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = True
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
End Sub
相关问题