如何在VBA中抓取电商网站商品图片并显示在Excel F列?
How to Scrape and Insert Product Images into Excel Column F with VBA
Your current code tries to assign element.innerText to column F for elements with class product-picture, but this won’t retrieve the actual image—since images are stored in <img> tags inside that div. Here’s how to fix it:
Key Fixes & Steps:
- Extract the image URL from the
<img>tag’ssrcattribute within theproduct-picturecontainer. - Use Excel’s
Shapes.AddPicturemethod to insert the image directly into the corresponding cell in column F.
Modified Working Code:
Dim site_Path As String Dim HTTPreq As New MSXML2.XMLHTTP60 Dim html As HTMLDocument Dim url As String Dim imgElement As Object ' Hold the img tag reference url = Sheet1.Range("I3").Text ' Send HTTP request to target URL With HTTPreq .Open "Get", url, False .send End With response = HTTPreq.responseText ' Load response into HTML document Set html = CreateObject("htmlfile") html.body.innerHTML = response r = 1 For Each divElement In html.getElementsByClassName("product-detail-description") r = r + 1 Set divCollection = divElement.all For Each element In divCollection If InStr(element.className, "product-name") > 0 Then Range("A" & r).Value = element.innerText If element.className = "salePrice" Then Range("B" & r).Value = element.innerText If element.className = "regPrice" Then Range("C" & r).Value = element.innerText If element.className = "product-new" Then Range("D" & r).Value = element.innerText If element.className = "line-level-primary-short-lrg llm-spill-short" Then Range("E" & r).Value = element.innerText ' Handle product image insertion If element.className = "product-picture" Then ' Grab the first img tag inside the product-picture div Set imgElement = element.getElementsByTagName("img")(0) If Not imgElement Is Nothing Then Dim imgUrl As String imgUrl = imgElement.src ' Insert image into column F, fit to cell dimensions With Sheet1.Shapes.AddPicture( _ Filename:=imgUrl, _ LinkToFile:=msoFalse, _ SaveWithDocument:=msoTrue, _ Left:=Sheet1.Range("F" & r).Left, _ Top:=Sheet1.Range("F" & r).Top, _ Width:=Sheet1.Range("F" & r).Width, _ Height:=Sheet1.Range("F" & r).Height) .Name = "ProductImg_" & r ' Name shape for future reference .LockAspectRatio = msoTrue ' Preserve image aspect ratio .Height = Sheet1.Range("F" & r).Height ' Resize to cell height End With End If End If Next element Next divElement
Critical Notes:
- Enable References: In the VBA Editor, go to Tools > References and check:
Microsoft XML, v6.0Microsoft HTML Object Library
- Relative URLs: If the
srcattribute is a path like/images/product.jpg, prepend the website’s base URL (e.g.,https://www.crateandbarrel.com) to form a full valid URL. - Error Handling: Add
On Error Resume NextorOn Error GoToblocks to skip missing images or invalid URLs without breaking the whole script. - Cell Sizing: Adjust column F’s width and row heights before running the script to ensure images display neatly.
内容的提问来源于stack exchange,提问作者Nooh_Noah
相关产品推荐
相关产品推荐

