Excel VBA / HTML 从下拉列表中单击下一页

Excel VBA / HTML Clicking next page from dropdown

本文关键字:单击 一页 下拉列表 VBA HTML Excel      更新时间:2023-09-26

我正在使用Excel和VBA编写我的第一个数据抓取器。 我被困在尝试转到网站的下一页时。源代码如下所示:

<li><a href="#" onclick="changePage(2); return false;">Page 2 of 24</a></li>

这是我拥有的VBA代码,但似乎不起作用:

For Each l In ie.Document.getElementsByTagName("a")
    If l.href = "#" And l.onclick = "changePage(2); return false;" Then
        l.Item(2).Click
        Exit For
    End If
Next l

当我运行代码时,我没有收到任何错误,但它似乎没有转到第 2 页。 请记住,第 2 页之后还有更多页面。 我的想法是稍后用变量替换"2"并将该变量增加 1。 但我需要先让它工作。

感谢任何可以提供帮助的人。

[编辑:我现在有一个解决方案,代码已被替换。

首先,我想提一下,如果以这种方式检索的数据用于商业目的或个人用途以外的任何用途,那么它违反了凯利蓝皮书 (kbb.com) 服务条款的 2 个部分。

仅供参考:像蓝皮书或MLS这样收集,更新和维护数据的网站非常重视他们的数据,他们不喜欢人们抓取它。我和我的一位老同学交谈,她拥有计算机科学学位,现在是一名房地产经纪人,我向她提到能够从 MLS 中抓取住房数据是多么酷,她几乎翻脸了我。只是说:人们被付钱来创建这些数据,人们使用这些数据谋生。"说得够多了。我能够通过在我自己的服务器上创建一个具有您正在寻找的相同格式的网页来运行问题代码,因为我在加拿大后得到了不同版本的 bluebook.com 网站。我被重定向到 kbb.com。

+++ 真正的问题 +++

问题是带有 # 符号的 hrefs 实际上是末尾附加了 # 的完整 URL,当您检查 onClick 事件时,它实际上包含完整的函数声明,因此您只需要搜索部分字符串。

' A good idea to declare the proper datatypes
' because IHTMLElement has the click event but IHTMLAnchorElements don't
Dim l As IHTMLElement
Dim htmlanchors As IHTMLElementCollection
' ...
Set htmlanchors = ie.Document.getElementsByTagName("a")
' Look through all the anchor tags on the page
    For Each l In htmlanchors
       ' Check to see the Href contains a # and the onclick event has specific code
        If InStr(l.href, "#") And InStr(l.onclick, "changePage(3); return false;") Then
            ' Click the current anchor link
            l.Click
            Exit For
        End If
Next l

你试过吗

.FireEvent ("onclick")
Or
.FireEvent ("onmouseover")
.FireEvent ("onmousedown")
.FireEvent("onmouseup")

代替.click ?有时 JavaScript 操作不会响应.click

Rick – 下面是我的整个代码。 我基本上是在尝试抓取www.the bluebook.com。

Sub ScrapeData()
Dim ie As InternetExplorer
Dim ele As Object
Dim RowCount As Long
Dim myWebsite As String, mySearch1 As String, mySearch2 As String, mySearch3 As String
Dim Document As HTMLDocument
myWebsite = Range("Website").Value
mySearch1 = Range("search1").Value
mySearch2 = Range("search2").Value
mySearch3 = Range("search3").Value
Set mySheet = Sheets("Sheet1")
Range("A6").Value = "Company"
Range("B6").Value = "Address"
Range("C6").Value = "Contact"
RowCount = 7
Set ie = New InternetExplorer
ie.Visible = True
With ie
.Visible = True
.navigate (myWebsite)
Do While .Busy Or .readyState <> 4
    DoEvents
Loop
ie.Document.getElementById("search").Value = mySearch1
ie.Document.getElementById("selRegion").Value = mySearch2
ie.Document.getElementsByClassName("searchBtn")(0).Click
Do While .Busy Or _
    .readyState <> 4
    DoEvents
Loop
For Each ele In .Document.all
    Select Case ele.className
    Case "result_title"
    RowCount = RowCount + 1
    Case "cname"
    mySheet.Range("A" & RowCount) = ele.innerText
    Case "addy_wrapper"
    mySheet.Range("B" & RowCount) = ele.innerText
    End Select
Next ele
End With
'THIS IS THE CODE THAT IS NOT WORKING
For Each l In ie.Document.getElementsByTagName("a")
    If l.href = "#" And l.onclick = "changePage(3); return false;" Then
        l.Item(3).Click
        Exit For
    End If
Next l
Set ie = Nothing
End Sub