首页 文章

在网页中搜索文本文件,并使用vba将该文本文件中的数据导入excel

提问于
浏览
0

我有一个网页,该网页中有一个文本文件 . 文本文件名每周都在不断变化 .

如何在网页中搜索文本文件并将内容下载到Excel工作表中 . 任何指针从哪里开始?我知道如何使用给定的网址导航到网页 .

问题是我不知道这个文本文件的URL(因为它每周都在不断变化),所以我怎么知道这个?

http://usda.mannlib.cornell.edu/MannUsda/viewDocumentInfo.do?documentID=1048

这是网页的链接 . 我需要在这里下载txt文件(Crop Progress,11.24.2014 [txt]) . 请帮帮我 .

录制的宏:

Sub Macro4()
 With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://usda.mannlib.cornell.edu/usda/current/CropProg/CropProg-11-24-2014.txt" _
        , Destination:=Range("$A$1"))
        .CommandType = 0
        .Name = "CropProg-11-24-2014"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
End Sub

1 回答

  • 0

    首先在您从VBA控制的Internet Explorer实例中打开网页(来自Microsoft Internet Controls库的ShDocVw.InternetExplorer对象) . InternetExplorer对象具有成员“文档”,其中包含保存网页上所有HTML标记的文档对象模型(DOM) . 使用MSHTML库来处理DOM .

    您可以在DOM中搜索包含文本“txt”的锚元素 . 您可以使用document.SelectNodes()中的XPath查询执行此操作 . 如果您不想使用XPath,则可以遍历document.all的元素或使用GetElementsByTagName() .

    或者,您可能会发现每次页面上的链接都是相同的(没有添加或删除),因此文本文件的链接始终位于document.getelementsbytagname(“a”)返回的数组中的相同位置 . 即使用类似document.GetElementsByTagName(“a”)(12)的东西,如果它始终是页面上的第13个锚标记 .

    获得对文本文件超链接的引用后,该URL将位于该锚元素的href属性中 .

相关问题