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

使用WinHTTP进行Excel网页爬取失败,求助获取产品名及图片URL

问题诊断与解决方案

核心问题

目标网站https://www.outre.com/new/的产品内容是JavaScript动态渲染生成的,你用WinHTTP获取的只是页面初始的静态HTML源码,其中并不包含实际的产品元素(比如你尝试定位的带uppercase.font-bold.text-[16px].xs:text-[1.3vw].lg:text-[14px]类的元素),所以无论用class、id还是其他选择器都无法找到目标内容。


解决方案1:使用Internet Explorer对象(支持JS渲染)

改用IE对象加载页面,它会等待JavaScript执行完成并渲染出完整内容,之后再进行元素定位:

Sub WebScrapingWithIE()
    Dim ie As Object
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = False ' 可设为True查看加载过程
    
    Dim url As String
    url = "https://www.outre.com/new/"
    
    ie.Navigate url
    ' 等待页面加载完成(含JS渲染)
    Do While ie.Busy Or ie.ReadyState <> 4
        DoEvents
    Loop
    
    Dim htmlDoc As Object
    Set htmlDoc = ie.Document
    
    ' 定位产品名称元素(需用浏览器开发者工具确认渲染后的真实类名)
    Dim elements As Object
    Set elements = htmlDoc.getElementsByClassName("product-title") ' 示例类名,需替换为实际值
    
    If elements.Length > 0 Then
        Dim i As Integer
        i = 1
        Dim element As Object
        For Each element In elements
            Sheets("Sheet1").Range("A" & i).Value = element.innerText
            ' 同步获取图片URL
            Dim imgElement As Object
            Set imgElement = element.ParentNode.QuerySelector("img") ' 假设图片在标题父节点内
            If Not imgElement Is Nothing Then
                Sheets("Sheet1").Range("B" & i).Value = imgElement.src
            End If
            i = i + 1
        Next
    Else
        Sheets("Sheet1").Range("A1").Value = "未找到产品元素"
    End If
    
    ie.Quit
    Set ie = Nothing
    Set htmlDoc = Nothing
End Sub

解决方案2:直接抓取产品数据API(更高效)

电商网站通常通过API接口返回产品数据,可通过浏览器开发者工具找到对应接口:

  1. 打开目标网站按F12进入开发者工具
  2. 切换到「网络」标签,刷新页面后筛选「XHR/Fetch」请求
  3. 找到返回产品列表的API(URL通常含products/api关键词)

找到API后,直接用WinHTTP请求获取JSON数据,解析后提取内容(需先导入VBA-JSON库):

Sub WebScrapingWithAPI()
    Dim objWinHTTP As Object
    Set objWinHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    
    ' 替换为实际找到的产品API地址
    Dim apiUrl As String
    apiUrl = "https://www.outre.com/api/products/new"
    
    objWinHTTP.Open "GET", apiUrl, False
    ' 添加请求头模拟浏览器
    objWinHTTP.SetRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
    objWinHTTP.send
    
    Dim jsonText As String
    jsonText = objWinHTTP.responseText
    
    ' 解析JSON
    Dim jsonObj As Object
    Set jsonObj = JsonConverter.ParseJson(jsonText)
    
    Dim product As Object
    Dim i As Integer
    i = 1
    For Each product In jsonObj("products") ' 需根据API返回的JSON结构调整路径
        Sheets("Sheet1").Range("A" & i).Value = product("title") ' 产品名称
        Sheets("Sheet1").Range("B" & i).Value = product("image_url") ' 图片URL(需匹配实际字段)
        i = i + 1
    Next
    
    Set objWinHTTP = Nothing
    Set jsonObj = Nothing
End Sub

注意事项

  • IE方案需确保浏览器已启用,等待逻辑要覆盖网络延迟场景,避免未渲染完成就抓取
  • API方案要注意请求频率限制,避免触发反爬;需根据实际返回的JSON结构调整字段路径
  • 避免使用动态生成的类名(比如你之前用的Tailwind CSS类),这类类可能随版本变化,建议用稳定的元素层级或固定类名定位

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 14:25:58