excel vba复制fedex跟踪信息

时间:2014-04-07 16:59:12

标签: excel vba dom web-scraping

我想复制到跟踪包裹时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

1 个答案:

答案 0 :(得分: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