从网站获取数据错误

时间:2016-08-03 12:29:42

标签: excel excel-vba vba

Sub GetDataFromLandings()
    Dim lastrow As Long, x As Long

    Application.ScreenUpdating = False

    ActiveWorkbook.Sheets("Sheet2").Range("A2:F250") = Null

    With Worksheets("Sheet2")
        lastrow = .Range("G" & Rows.Count).End(xlUp).Row

        For x = 2 To lastrow
            RequeryLandings .Cells(x, "G")
        Next
    End With
    Application.ScreenUpdating = True
End Sub

Sub RequeryLandings(address As String)
    Dim ws As Worksheet
    Dim NewRow As Long

    With Worksheets("Sheet2")

        Set ws = ActiveWorkbook.Sheets("Sheet1")

        With ws.QueryTables.Add(Connection:= _
        "URL;https://www.flightradar24.com/data/aircraft/" & address, _
        Destination:=ws.Range("$A$1"))
            .Name = "NNum_Results.aspx?NNumbertxt=22NA"
            .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

            DoEvents

以上代码从网站https://www.flightradar24.com/data/aircraft/

获取数据

并循环显示从G列中获取的不同值:

RequeryLandings .Cells(x, "G")

所以URL地址是这种形式

"URL;https://www.flightradar24.com/data/aircraft/" & address,

其中& address是G列的值。

如果G列中的第一行是 SP-LRC ,那么它会搜索网址

"URL;https://www.flightradar24.com/data/aircraft/SP-LRC"

我正在尝试从不同的网站获取数据

http://www.airliners.net/search?registrationActual=SP-LRC&display=detail

我无法弄清楚代码应该如何更改才能正常使用该地址...在这种情况下,SP-LRC不在地址的末尾,而是在&display=detail之前

1 个答案:

答案 0 :(得分:1)

string

http://www.airliners.net/search?registrationActual=SP-LRC&display=detail

可以写成

"http://www.airliners.net/search?registrationActual=" & "SP-LRC" & "&display=detail"

所以改变

"URL;https://www.flightradar24.com/data/aircraft/" & address

"URL;http://www.airliners.net/search?registrationActual=" & address & "&display=detail"
相关问题