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

时间:2016-02-18 23:13:58

标签: javascript html excel-vba web-scraping vba

我正在使用Excel和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

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

{{1}}

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

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

3 个答案:

答案 0 :(得分:2)

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

首先,我想提一下,如果以这种方式检索的数据用于商业用途或个人使用以外的任何其他用途,则违反了Kelley Blue Book(kbb.com)服务条款的两个部分。

仅供参考:收集,更新和维护BlueBook或MLS等数据的网站非常重视他们的数据,他们不喜欢人们抓取它。我和我的一位老同学说话,她拥有计算机科学学位,现在是一名房地产经纪人,我向她提到了从MLS上刮下住房数据是多么酷,她几乎翻了个身。 。只是说:人们获得了创造数据的报酬,人们利用这些数据谋生。 ' Nuff说。 我能够通过在我自己的服务器上创建一个网页来运行问题代码,因为我在加拿大以来获得了不同版本的bluebook.com网站。我被重定向到kbb.com。

+++真正的问题+++

问题是带有#符号的href实际上是带有#末尾的完整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

答案 1 :(得分:0)

你试过吗

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

取代.click?有时,JavaScript操作无法响应.click

答案 2 :(得分:0)

瑞克 - 下面是我的整个代码。我基本上试图刮掉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