仅将特定表从网站提取到Excel中

时间:2012-10-20 15:00:50

标签: excel vba excel-vba

我需要使用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中。

2 个答案:

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

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