You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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’s src attribute within the product-picture container.
  • Use Excel’s Shapes.AddPicture method 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:

  1. Enable References: In the VBA Editor, go to Tools > References and check:
    • Microsoft XML, v6.0
    • Microsoft HTML Object Library
  2. Relative URLs: If the src attribute 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.
  3. Error Handling: Add On Error Resume Next or On Error GoTo blocks to skip missing images or invalid URLs without breaking the whole script.
  4. Cell Sizing: Adjust column F’s width and row heights before running the script to ensure images display neatly.

内容的提问来源于stack exchange,提问作者Nooh_Noah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 15:50:36