【发布时间】:2018-09-28 02:17:45
【问题描述】:
在使用 getelementsbyClassName 时,我不断收到错误消息“对象不支持此属性或方法”。我想获取来自搜索引擎百度(Baidu.com)的网页的所有网址。但我无法“点击”搜索引擎的下一页。我已经尝试了几天,如果有人可以帮助我,那就太好了。谢谢!
Option Explicit
Public Sub GettingURL()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
Dim c As Long, u As Long
c = 3
Dim e
Dim PageCount As Long
With ie
.Visible = False
.navigate "http://www.baidu.com"
'searching for the word on the search enigne
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
.document.getElementsByTagName("INPUT")("wd").Value = Sheets("Data").Cells(1, 2).Value
.document.getElementById("su").Click
'getting result for page 1 to 20
For PageCount = 1 To 20
'loop until the end to get resulting url and navigate to it
Do
Loop Until .readyState = READYSTATE_COMPLETE
.navigate .document.URL
'navigating page and this line causes problem:
.document.GetelementByClassName("pc")(0) = PageCount
'getting the url of all the search result in the result page
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
With .document.querySelectorAll("#content_left h3 [href]")
For u = 0 To 50
Sheets("Data").Cells(c, 1) = .Item(u)
c = c + 1
Next
End With
PageCount = PageCount + 1
Next
.Quit
End With
End Sub
【问题讨论】:
标签: vba excel web-scraping