编辑:所以这个对我有用。。我现在想把它转换成我的目的。我试图将其切换为标题和价格,但是当我转换行时: 设置链接= .document.querySelectorAll(“。s-item__link [href]”) 至 设置链接= .document.querySelectorAll(“。s-item__title [h3]”)
它为每一行拉“ [object HTMLHeadingElement]”-思想?
Option Explicit
Public Sub GetInfo()
Dim ie As New InternetExplorer, ws As Worksheet, t As Date
Const MAX_WAIT_SEC As Long = 10
Set ws = ThisWorkbook.Worksheets("Sheet1")
With ie
.Visible = True
.Navigate2 "https://www.ebay.com/"
While .Busy Or .readyState < 4: DoEvents: Wend
ie.document.querySelector("#gh-ac").Value = "Bike"
ie.document.querySelector("#gh-btn").Click
While .Busy Or .readyState < 4: DoEvents: Wend
Dim links As Object, i As Long, count As Long
t = Timer
Do
On Error Resume Next
Set links = .document.querySelectorAll(".s-item__link[href]")
count = links.Length
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While count = 0
For i = 0 To links.Length - 1
ws.Cells(i + 1, 1) = links.item(i)
Next
.Quit
End With
结束子
答案 0 :(得分:1)
这里是在ebay上搜索Bike并循环播放直到链接出现的示例
Option Explicit
Public Sub GetInfo()
Dim ie As New InternetExplorer, ws As Worksheet, t As Date
Const MAX_WAIT_SEC As Long = 10
Set ws = ThisWorkbook.Worksheets("Sheet1")
With ie
.Visible = True
.Navigate2 "https://www.ebay.com/"
While .Busy Or .readyState < 4: DoEvents: Wend
ie.document.querySelector("#gh-ac").Value = "Bike"
ie.document.querySelector("#gh-btn").Click
While .Busy Or .readyState < 4: DoEvents: Wend
Dim links As Object, i As Long, count As Long
t = Timer
Do
On Error Resume Next
Set links = .document.querySelectorAll(".s-item__link[href]")
count = links.Length
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While count = 0
For i = 0 To links.Length - 1
ws.Cells(i + 1, 1) = links.item(i)
Next
.Quit
End With
End Sub