【问题标题】:excel vba copying fedex tracking informationexcel vba复制fedex跟踪信息
【发布时间】:2014-04-07 16:59:11
【问题描述】:

我想将website 在我跟踪包裹时生成的跟踪信息表复制到 Excel 3 中。我想通过 Excel VBA 来完成。我可以编写一个循环并为各种跟踪号码生成此网页。但我很难复制表格 - 顶级表格、旅行历史和货运跟踪表。有什么解决办法吗?在我的 vba 代码中,下面的最后 3 行给出了错误:( - 运行时错误 '438' 对象不支持此属性或错误。

Sub final()
Application.ScreenUpdating = False
Set ie = CreateObject("InternetExplorer.Application")
my_url = "https://www.fedex.com/fedextrack/index.html?tracknumbers=713418602663&cntry_code=us"

    With ie
        .Visible = True
        .navigate my_url


    Do Until Not ie.Busy And ie.readyState = 4
        DoEvents
    Loop

    End With

ie.document.getElementById("detailsBody").Value
ie.document.getElementById("trackLayout").Value 
ie.document.getElementById("detail").Value

End Sub

【问题讨论】:

    标签: excel vba dom web-scraping


    【解决方案1】:

    .Value 不是该上下文中可用的方法。此外,您还需要将方法调用的返回值分配给一个变量。另外,你应该声明你的变量:)

    我做了一些修改,包括了一种从其中一个表中获取数据的可能方式。您可能需要使用 TextToColumns 或类似方法重新格式化输出,因为它会在单个单元格中打印每一行。

    我还注意到,当我执行此操作时,表格有时还没有完成加载,除非您输入合适的 Wait 或使用其他方法来确定数据何时完全加载到网页。我用一个简单的Application.Wait

    Option Explicit
    
    Sub final()
    Dim ie As Object
    Dim my_url As String
    Dim travelHistory As Object
    Dim history As Variant
    Dim h As Variant
    Dim i As Long
    
    
    
    Application.ScreenUpdating = False
    Set ie = CreateObject("InternetExplorer.Application")
    my_url = "https://www.fedex.com/fedextrack/index.html?tracknumbers=713418602663&cntry_code=us"
    
    
        With ie
            .Visible = True
            .navigate my_url
            '## I modified this logice a little bit:
            Do While .Busy And .readyState <> 4
                DoEvents
            Loop
    
        End With
    
    '## Here is a simple method wait for IE to finish, you may need a more robust solution
    '   For assistance with that, please ask a NEW question.
    
    Application.Wait Now() + TimeValue("0:00:10")
    
    
    '## Get one of the tables
    Set travelHistory = ie.Document.GetElementByID("travel-history")
    '## Split teh table to an array
    history = Split(travelHistory.innerText, vbLf)
    '## Iterate the array and write each row to the worksheet
    For Each h In history
        Range("A1").Offset(i).Value = h
        i = i + 1
    Next
    
    
    ie.Quit
    Set ie = Nothing
    End Sub
    

    【讨论】:

    • 如何将这些表格的内容复制到 Excel 中?是否可以修改您的代码以显示如何将数据粘贴到 excel“Sheet1”列 A、B 和 C?
    • 您提供的代码实际上并未捕获表格的内容。我会看看是否有一个快速解决方案。同时,由于这解决了438错误的直接问题,请考虑对此答案进行投票。
    • 查看显示提取表数据的一种简单方法的修订版。您可能需要对其进行微调以进行格式化。与此相关的问题应该是一个新问题,请:)
    猜你喜欢
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 2016-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多