3

我需要使用 VBA 从http://www.zillow.com/homes/comps/67083361_zpid/获取表格到 Excel 中。我只想要那张桌子,别无其他。但是当我使用时:

Set objIE = CreateObject("InternetExplorer.Application")

With objIE
    .Visible = True
    .Navigate "http://www.zillow.com/homes/comps/67083361_zpid/"
    Do While .ReadyState <> 4: DoEvents: Loop
    Debug.Print .document.Body.outerText
End With

它给了我这样的文字:

4723 N 63rd Dr$63,50008/17/201241.752,0747,6751972$360.11

对于我无法分析并存储到 Excel 不同单元格中的每种产品。

那么有没有一种方法可以以一种可管理的方式获取页面数据。如果我需要为此遍历一个循环,我可以。我还可以进行额外的处理以正确地将行数据填充到 Excel 中。

4

2 回答 2

11

我会使用下面的,因为我发现查询表很慢而且 IE 非常慢;)

Sub GetData()
    Dim x As Long, y As Long
    Dim htm As Object

    Set htm = CreateObject("htmlFile")

    With CreateObject("msxml2.xmlhttp")
        .Open "GET", "http://www.zillow.com/homes/comps/67083361_zpid/", False
        .send
        htm.body.innerhtml = .responsetext
    End With

    With htm.getelementbyid("comps-results")
        For x = 0 To .Rows.Length - 1
            For y = 0 To .Rows(x).Cells.Length - 1
                Sheets(1).Cells(x + 1, y + 1).Value = .Rows(x).Cells(y).innertext
            Next y
        Next x
    End With

End Sub
于 2012-10-22T08:36:39.393 回答
5

我已经使用以下代码完成了它:

Sub FetchData()
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://www.zillow.com/homes/comps/67083361_zpid", Destination:=Range( _
        "$A$1"))
        .Name = "67083361_zpid"
        .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
于 2012-10-20T15:45:39.497 回答