从zoopla.co.uk获取数据

时间:2013-11-07 12:00:55

标签: excel vba excel-vba

我正在尝试创建一个查看电子表格中单元格的函数,并返回Zoopla' Z-Index'来自页面http://www.zoopla.co.uk/market/uk/。请注意,URL中的 market 部分是唯一一个更改的位,例如BS8,BS6等,这应该从我的电子表格中的选定单元格中获取。然后它应该返回Z-Index值,这是All,Detached,Semi和Terrace的平均房产价格。

这是我到目前为止所创造的。

我写的代码并没有从单元格A1中提取邮政编码的平均属性数字。有什么想法吗?

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = Range.("L:L").Row And _
   Target.Column = Range.("L:L").Column Then 
    Dim IE As New InternetExplorer
    IE.Visible = False
    IE.navigate "www.zoopla.co.uk/market/" & Range("A1/").Value
    Do
        DoEvents
    Loop Until IE.readyState = READYSTATE_COMPLETE
    Dim Doc As HTMLDocument
    Set Doc = IE.document
    Dim sSPAN As String
    sSPAN = Trim(Doc.getElementsByTagName("span")(1).innerText)
    sSPAN = Split(sSPAN, vbNewLine)(0)
    Range("O:O").Value = Split(sSPAN,", ")(0)
    Range("P:P").Value = Split(sSPAN,", ")(1)
End If
End Sub![Data needed][1]

1 个答案:

答案 0 :(得分:4)

你的脚本有一些错误,我已经纠正了一些行,它有效:

Private Sub Worksheet_Change(ByVal Target As Range)
  Const READYSTATE_COMPLETE = 4

  Dim j
  Dim xcolElements
  Dim Doc, el, IE

  '
  ' if the content of the cell L1 changes:
  '
  If Target.Row = Range("L1").Row And _
     Target.Column = Range("L1").Column Then

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = False
    IE.Navigate "http://www.zoopla.co.uk/market/" & Range("A1").Value & "/"
    Do
      DoEvents
    Loop Until Not IE.Busy And IE.ReadyState = READYSTATE_COMPLETE

    Set Doc = IE.Document

    Set xcolElements = Doc.getElementsByClassName("zoopla")
    '
    j = 1
    For Each el In xcolElements
      Range("O" & j).Value = el.FirstChild.Data
      j = j + 1
    Next
    '
    IE.Quit
    '
  End If
  '
  ' destruct objects:
  '
  Set el = Nothing
  Set xcolElements = Nothing
  Set Doc = Nothing
  Set IE = Nothing
  '
End Sub

enter image description here

你知道VBA + IE是如此强大,以至于你可以通过调整上述VBA代码来做很多事情。

我们使用

Set IE = CreateObject("InternetExplorer.Application")

而不是

Set IE = new InternetExplorer.Application

避免添加InternetExplorer.Application ActiveX引用,从而最大限度地减少RAM需求 在没有使用IE自动化的情况下打开文档时。

'
' to fill C12, C13, C14,...
'
j = 12
For Each el In xcolElements
  Range("C" & j).Value = el.FirstChild.Data
  j = j + 1
Next