从VBA刮刮 - 刮刀已经可以处理一些数据而不是其他数据

时间:2015-01-07 10:38:40

标签: vba web-scraping

我使用本网站的代码从属性网站Foxtons中提取数据,代码如下

Sub foxtons()

Const READYSTATE_COMPLETE = 4

Dim j
Dim xcolElements
Dim Doc, ell, ie
Dim pn
j = 1
For pn = 1 To 10
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
ie.Navigate "http://www.foxtons.co.uk/search?current_page=" & pn & "&location_ids=288&search_form=map&search_type=SS&sold=1&submit_type=search"
Do
  DoEvents
Loop Until Not ie.Busy And ie.readyState = READYSTATE_COMPLETE

Set Doc = ie.Document

Set xcolElements = Doc.getElementsByClassName("description")
'Here lies the problem. Description works perflectly, price doesn't

For Each ell In xcolElements
  Range("B" & j).Value = ell.FirstChild.Data
  j = j + 1
Next
Next pn
ie.Quit
Set el = Nothing
Set xcolElements = Nothing
Set Doc = Nothing
Set ie = Nothing
End Sub

上面的代码可以工作,并且应该将600个属性描述引入B列。当我将“描述”切换到价格时会出现问题。在这里,我收到一条错误消息,但这对我来说可能更重要

1 个答案:

答案 0 :(得分:0)

使用此代码检索价格元素:

Private Sub CommandButton1_Click()

    Const READYSTATE_COMPLETE = 4

    Dim j As Integer

    Dim ie As InternetExplorer
    Dim Doc As IHTMLDocument
    Dim xcolElements As IHTMLElementCollection
    Dim ell As IHTMLElement

    Dim pn As Integer

    j = 1
    Set ie = CreateObject("InternetExplorer.Application")

    For pn = 1 To 10

        ie.Visible = True
        ie.Navigate "http://www.foxtons.co.uk/search?current_page=" & pn & "&location_ids=288&search_form=map&search_type=SS&sold=1&submit_type=search"
        Do
          DoEvents
        Loop Until Not ie.Busy And ie.readyState = READYSTATE_COMPLETE

        Set Doc = ie.Document

        'Set xcolElements = Doc.getElementsByClassName("description")
        'Here lies the problem. Description works perflectly, price doesn't
        Set xcolElements = Doc.getElementsByClassName("price")



        For Each ell In xcolElements
          On Error GoTo Skip
          Range("B" & j).Value = ell.Children(0).Children(1).innerText
          j = j + 1
Skip:
        Next ell 

    On Error Goto 0

    Next pn

    ie.Quit
    Set el = Nothing
    Set xcolElements = Nothing
    Set Doc = Nothing
    Set ie = Nothing

End Sub

祝你找到新家好运:)