Excel VBA / HTML 从下拉列表中单击下一页
Excel VBA / HTML Clicking next page from dropdown
我正在使用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
- 单击一次后禁用按钮-第2部分
- 如何对每个用户每天只允许单击一次的按钮进行编码
- jQuery Datatables:单击一列时对另一列进行排序
- 如何在不刷新页面的情况下每次单击一组选项卡时刷新视图内容窗格
- 如何在jRate插件上设置readOnly;单击一颗星后的参数
- 为每个字母单击一次
- 对同一类中的所有元素单击一次
- 单击一次以静音,另一次单击可取消静音
- 如何加载一个html页面并在其中只需单击一次即可运行函数
- 在FullCalendar中单击一天获取事件
- 只需单击一次按钮,jquery即可自动淡入/淡出文本
- 菜单在每单击一次时切换不同的动画
- 双击时设置内容可编辑 - IE 不允许编辑,除非再单击一次
- 允许按钮在流星中只单击一次
- PHP 在输入内单击一次
- 如何创建只能单击一次的点击处理程序
- 当我在 javascript 中单击分页项时,我如何查看图片
- 每次单击一次迭代两个数组并制作动画
- 限制用户仅单击一次标记
- 单击一页滚动菜单时更改URL哈希值