首先,此代码行之有效,这要感谢QHarr进行了整理。当我仅使用代码中的网址进行抓取时,该代码可以很好地工作。
我终于得到它接受来自Sheet1 Cell A1的搜索条件。这样,我可以将搜索项放在A1中,并在IE打开时将其拉出。这使我永远地锻炼。
现在的问题是代码无法提取任何信息。唯一的更改是对网址
我现在已经在代码中保留了两个URL,有效的URL是活动的,而不是作为注释。当将搜索项放在A1中并且该页面在ebay中打开时,第二个url有效。目前,此网址显示为评论。
我无法弄清楚问题所在,除了我不得不将这部分网址保留为&_ sacat = 0 ,因为我不断收到错误消息“编译错误-预期结语”并且无法运行代码。
这是我的代码
enter Option Explicit
Public Sub GetDataEbay()
Dim htmlDoc As MSHTML.HTMLDocument, ie As SHDocVw.InternetExplorer, ws As Worksheet
Set ie = New SHDocVw.InternetExplorer
Set htmlDoc = New MSHTML.HTMLDocument
Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim nextPageElement As Object
Dim pageNumber As Long
With ie
.Visible = True
'Original Code for search, but only searches 1 item
.Navigate2 "https://www.ebay.co.uk/sch/i.html?_from=R40&_trksid=m570.l1313&_nkw=jackets&_sacat=0"
' Takes seach from A1 and places it into ebay
'.Navigate2 "https://www.ebay.co.uk/sch/i.html?_from=R40&_trksid=m570.l1313&_nkw=" & Replace(Worksheets("Sheet1").Range("A1").Value, " ", "+")
While .Busy Or .readyState <> 4: DoEvents: Wend
Dim index As Long, HTMLItems As Object, rowNum As Long, xCell As Range
Dim cssSelectors(), i As Long
Select Case True
Case InStr(.document.URL, "ebay.co.uk") > 0
cssSelectors = Array(".gvtitle a", ".amt", ".gvtitle a")
Case InStr(.document.URL, "ebay.com") > 0
cssSelectors = Array(".s-item__title", ".s-item__price", ".s-item__link")
End Select
With ws
For i = LBound(cssSelectors) To UBound(cssSelectors)
rowNum = 1
Set HTMLItems = ie.document.querySelectorAll(cssSelectors(i))
For index = 0 To HTMLItems.Length - 1
.Cells(rowNum, i + 1).Value = IIf(i = 2, HTMLItems.Item(index).getAttribute("href"), HTMLItems.Item(index).innerText)
rowNum = rowNum + 1
Next
Next
For Each xCell In .Range("C1:C25000") '<= all these really?
.Hyperlinks.Add Anchor:=xCell, Address:=xCell.Formula
Next xCell
End With
'.Quit
End With
End Sub
在这里编码
有人可以看一下吗,我尝试了几种URL变体以使其接受Sheet1 A1的搜索,而代码中的那个是唯一没有给我“编译错误-预期”的代码声明结束”消息,这也是我可以在ebay上调出搜索项的唯一网址变体。
我希望经验更丰富的人可以看到我所缺少的内容或更好地编写网址。这是唯一的问题。
感谢您的光临