Vic*_*man 10 html javascript vba excel-vba web-scraping
我正在使用Excel和VBA编写我的第一个数据抓取器.我一直试图去网站的下一页.源代码如下:
<li><a href="#" onclick="changePage(2); return false;">Page 2 of 24</a></li>
Run Code Online (Sandbox Code Playgroud)
这是我的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
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我没有收到任何错误,但它似乎没有转到第2页.请记住,第2页后面有更多页面.我的想法是稍后用变量替换"2"并增加变量一个.但我需要先让它工作.
感谢无论谁能提供帮助.
[编辑:我现在有了解决方案,并且代码已被替换。-RDH]
首先我想提一下,如果以这种方式检索到的数据用于商业目的或个人用途以外的任何用途,则违反了 Kelley Blue Book (kbb.com) 服务条款的 2 部分。
仅供参考:像 BlueBook 或 MLS 这样收集、更新和维护数据的网站非常重视他们的数据,他们不喜欢人们抓取数据。我正在和我的一位老同学聊天,她拥有计算机科学学位,现在是一名房地产经纪人,我向她提到能够从 MLS 上抓取住房数据是多么酷,她几乎对我大发雷霆。只是说:人们获得报酬来创建这些数据,人们使用这些数据来谋生。”纳夫说道。 我能够通过在我自己的服务器上创建一个网页来运行问题代码,该网页的格式与您正在寻找的格式相同,因为我在加拿大,所以得到了不同版本的 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
867 次 |
| 最近记录: |