【发布时间】:2019-11-27 12:18:54
【问题描述】:
我想从电子商务网站抓取产品标题、价格、卖家和图片网址。结果需要复制到 A-D 列的活动工作表中。下面的代码最初是由@QHarr 之一为亚马逊开发的。任何帮助更新它以获得所需的结果?
我已经包含了结果的样子。谢谢
Public Sub WriteOutProductInfo()
'VBE>Tools>References> Microsoft HTML Object Library
Dim html As MSHTML.HTMLDocument
Set html = New MSHTML.HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
' change the url for the page of amazon from where to copy data
.Open "GET", "https://www.daraz.lk/catalog/?from=input&q=sarees&ppath=31186:3287", False
.setRequestHeader "User-Agent", "Mozilla/5.0"
.send
html.body.innerHTML = .responseText
End With
' 1. declare additional headers as variable
Dim headers(), titles As Object, prices As Object, original_prices As Object
Dim seller As Object
headers = Array("Title", "Price")
With html
Set titles = .querySelectorAll(".c3gUW0,.c13VH6")
Set prices = .querySelectorAll(".------------")
End With
Dim results(), r As Long, priceInfo As String
ReDim results(1 To titles.Length, 1 To UBound(headers) + 1)
For r = 0 To titles.Length - 1
results(r + 1, 1) = titles.Item(r).innerText
Next
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
With ws
.Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
.Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
End With
End sub
【问题讨论】:
-
已编辑请取消标记为保留
标签: html excel vba web-scraping