【发布时间】:2019-05-26 15:23:04
【问题描述】:
我希望关注 A 列中的一系列 URL(例如:https://www.ebay.com/itm/Apple-iPhone-7-GSM-Unlocked-Verizon-AT-T-TMobile-Sprint-32GB-128GB-256GB/352381131997?epid=225303158&hash=item520b8d5cdd:m:mWgYDe4a79NeLuAlV-RmAQA:rk:7:pf:0)并从中提取以下信息: - 标题 - 价钱 - 描述
我认为我的代码存在多个问题...首先,我无法让程序遵循 Excel 中列出的特定 URL(仅当我在代码中指定一个时)。此外,拉多个字段给我带来了问题。
Option Explicit
Public Sub ListingInfo()
Dim ie As New InternetExplorer, ws As Worksheet, t As Date
Dim i As Integer
i = 0
Do While Worksheets("Sheet1").Cells(i, 1).Value <> ""
Const MAX_WAIT_SEC As Long = 10
Set ws = ThisWorkbook.Worksheets("Sheet1")
With ie
.Visible = True
.Navigate2 Worksheets("Sheet1").Cells(i, 1).Value
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 Title = .document.querySelectorAll("it-ttl")
Set price = .document.querySelectorAll("notranslate")
Set Description = .document.querySelectorAll("ds_div")
count = Links.Length
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While count = 0
For i = 0 To Title.Length - 1
ws.Cells(i + 1, 1) = Title.item(i)
ws.Cells(i + 1, 2) = price.item(i)
ws.Cells(i + 1, 3) = Description.item(i)
Next
.Quit
i = i + 1
Loop
End With
End Sub
【问题讨论】:
-
在您的
Do While的第一次迭代中,i将等于零 - 而且此代码不应编译,因为您的范围中有重复声明(您正在声明 @ 987654325@ 两次)。 -
您也在使用
Option Explicit,但我看不到您在哪里声明Title、price、Description- 我在这里缺少什么?这个潜艇甚至不应该能够运行。