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

Excel VBA通过IE抓取沃尔玛页面源码遇问题求助

沃尔玛商品页面爬取问题及解决方案建议

我尝试获取以下沃尔玛商品页面的信息:

view-source:https://www.walmart.com/ip/Rice-Krispies-Treats-Original-Chewy-Crispy-Marshmallow-Squares-Ready-to-Eat-12-4-oz-16-Count/10818666?athbdg=L1600&from=/search

测试多种代码后,目前遇到两个核心问题:

  • 用Internet Explorer对象打开页面时,网站提示不支持Internet Explorer
  • 偶尔出现210错误,怀疑是公司安全政策导致,但无从下手

当前使用的VBA代码如下:

Option Explicit
Sub WalmartDoesntWorkWithBrowser()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://www.walmart.com/ip/Rice-Krispies-Treats-Original-Chewy-Crispy-Marshmallow-Squares-Ready-to-Eat-12-4-oz-16-Count/10818666?athbdg=L1600&from=/search"
Do Until IE.ReadyState = 4
DoEvents
Loop
Debug.Print IE.Document.DocumentElement.OuterHTML
End Sub

针对IE不支持的解决方案

1. 改用Edge浏览器自动化

微软已停止IE的官方支持,沃尔玛网站也不再兼容该浏览器。可以直接调用Edge对象实现页面自动化,示例代码:

Option Explicit
Sub UseEdgeAutomation()
Dim edge As Object
Set edge = CreateObject("Microsoft.Edge.Application")
edge.Visible = True
edge.Navigate "https://www.walmart.com/ip/Rice-Krispies-Treats-Original-Chewy-Crispy-Marshmallow-Squares-Ready-to-Eat-12-4-oz-16-Count/10818666?athbdg=L1600&from=/search"
' 等待页面加载完成
Do While edge.Busy Or edge.ReadyState <> 4
    DoEvents
Loop
Debug.Print edge.Document.DocumentElement.OuterHTML
End Sub

注:需确保本地已安装Edge浏览器,且系统已注册Microsoft.Edge.Application对象。

2. 直接发送HTTP请求(绕过浏览器)

避免浏览器兼容性问题,用VBA发送GET请求直接获取页面源码,示例代码:

Option Explicit
Sub UseHTTPRequest()
Dim xmlHttp As Object
Set xmlHttp = CreateObject("MSXML2.XMLHTTP.6.0")
xmlHttp.Open "GET", "https://www.walmart.com/ip/Rice-Krispies-Treats-Original-Chewy-Crispy-Marshmallow-Squares-Ready-to-Eat-12-4-oz-16-Count/10818666?athbdg=L1600&from=/search", False
' 模拟浏览器请求头,降低反爬识别概率
xmlHttp.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 Edg/118.0.2088.61"
xmlHttp.send

If xmlHttp.Status = 200 Then
    Debug.Print xmlHttp.responseText
Else
    Debug.Print "请求失败,状态码:" & xmlHttp.Status & " 错误信息:" & xmlHttp.statusText
End If
Set xmlHttp = Nothing
End Sub

针对210错误的排查建议

  1. 检查公司代理/防火墙设置:210错误多与网络拦截有关,若公司网络需要代理,可在HTTP请求代码中添加代理配置:
xmlHttp.setProxy 2, "代理地址:端口" ' 示例:"proxy.company.com:8080"
  1. 补充更多请求头:除User-Agent外,添加Referer、Accept等头,进一步模拟正常浏览器行为:
xmlHttp.setRequestHeader "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
xmlHttp.setRequestHeader "Referer", "https://www.walmart.com/search?q=rice+krispies+treats"
  1. 切换网络环境测试:用非公司网络(如手机热点)运行代码,确认是否为公司安全政策导致的拦截。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 02:25:27