使用 VBA 从网页中获取数据到 Excel

问题描述:

我正在尝试从以下网页中提取数据.

I am trying to pull data from the following web page.

https://pro.calnea.com/login

用户:tyronr@outlook.com

User: tyronr@outlook.com

通过:calnea1

登录后,通过Pro Services"并单击Comps Search (Photo)".我已经输入了一个邮政编码并已经勾选了几个属性,这些属性应该将它们放入Shortlist.要访问短名单,请转到页面底部和右侧,有一个显示查看短名单"的按钮,单击该按钮.现在您看到了选定的属性,我想为每个属性提取每条数据,例如,单元格 A1 = 地址、A2 = 最后销售价格、A3 = 最后销售日期等一直到状态.然后是下一行的下一个属性,所以 B1 = 地址等.如果可能的话,我也想获取图像 URL.

Once logged in hoover over "Pro Services" and click "Comps Search (Photo)". I have typed a post code in and ticked a couple of properties already which should put them in the Shortlist. To access the short list go to the bottom on the page and on the right, there is a button which says "View Shortlist", click that. Now you see the selected properties and I want to pull each piece of data for each property, for example, cell A1 = Address, A2 = Last Sales Price, A3 = Last Sales Date etc all the way to Status. Then the next property on the next line so B1 = Address etc. If possible I want to get the image URL as well.

我不确定解决此问题的最佳方法,因为有一个登录,但我在浏览器上保持登录状态,所以我认为这不是问题?

I am not sure of the best way around this as there is a log in however I am remaining logged in on the browser so I assume this isnt an issue?

以下是我到目前为止所拥有的,但不幸的是我没有运气,非常感谢您的帮助!:)

Below is what I have so far but unfortunately I am having no luck and help would be MUCH appreciated! :)

Sub test()
Dim eRow As Long
Dim ele As Object
Set sht = Sheets("Sheet1")
RowCount = 1
sht.Range("A" & RowCount) = "Address"
sht.Range("B" & RowCount) = "Last Sales Price"
sht.Range("C" & RowCount) = "Last Sales Date"
sht.Range("D" & RowCount) = "Property Type"

eRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

Set objIE = CreateObject("InternetExplorer.Application")


With objIE
.Visible = True
.navigate "http://pro.calnea.com/client/cmp_shortlist/bs6?Require_EA=false&SearchMode=CMP"
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
Set what = .document.getElementsByName("q")
what.Item(0).Value = Address
Set Address = .document.getElementsByName("where")
Address.Item(0).Value = Last Sales Price
.document.getElementById("View Shortlist").Click
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
For Each ele In .document.all
Select Case ele.classname
Case "Result"
RowCount = RowCount + 1
Case "Title"
sht.Range("A" & RowCount) = ele.innertext
Case "Company"
sht.Range("B" & RowCount) = ele.innertext
Case "Location"
sht.Range("C" & RowCount) = ele.innertext
Case "Description"
sht.Range("D" & RowCount) = ele.innertext
End Select
Next ele
End With
Macro1
Set objIE = Nothing
End Sub

我看到了来自 从 zoopla.co.uk 获取数据,我刚刚更正了一些 VBA 语法错误,请检查:

I seen the link from Fetch data from zoopla.co.uk, I just corrected some VBA syntax error, please check this:

Sub sofFetchDataFromWebPage()
  Dim RowCount, eRow As Long
  Dim sht, ele As Object, what, Address
  Dim objIE

'
  Set sht = Sheets("Sheet1")
'
' Set sht = ActiveSheet

  RowCount = 1
  sht.Range("A" & RowCount) = "Address"
  sht.Range("B" & RowCount) = "Last Sales Price"
  sht.Range("C" & RowCount) = "Last Sales Date"
  sht.Range("D" & RowCount) = "Property Type"

  eRow = sht.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

  Set objIE = CreateObject("InternetExplorer.Application")

  With objIE
    .Visible = True
    .Navigate "http://pro.calnea.com/client/cmp_shortlist/bs6?Require_EA=false&SearchMode=CMP"
    Do While .Busy Or .readyState <> 4
      DoEvents
    Loop
    Set what = .document.getElementsByName("q")
    what.Item(0).Value = "Address"
    Set Address = .document.getElementsByName("where")
    Address.Item(0).Value = "Last Sales Price"
    .document.getElementById("View Shortlist").Click
    Do While .Busy Or .readyState <> 4
      DoEvents
    Loop
    For Each ele In .document.all
      Select Case ele.classname
        Case "Result"
          RowCount = RowCount + 1
        Case "Title"
         sht.Range("A" & RowCount) = ele.innertext
        Case "Company"
          sht.Range("B" & RowCount) = ele.innertext
        Case "Location"
          sht.Range("C" & RowCount) = ele.innertext
        Case "Description"
          sht.Range("D" & RowCount) = ele.innertext
      End Select
    Next
  End With
  Set objIE = Nothing
End Sub

因为您已经在另一个 IE 窗口登录过,所以您可能不需要在这里再次登录.

As you has logged in in another IE Window, you may not need do login here again.