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

使用PowerShell调用Investing接口获取历史数据时触发403错误求助

解决PowerShell脚本调用Investing.com接口返回403 Forbidden的问题

问题分析

403禁止错误通常是因为网站反爬机制更新,原脚本的请求标识、头信息或会话验证不符合当前网站的访问规则。

修复方案

1. 更新User-Agent为现代浏览器标识

原脚本使用的Firefox 33.0版本过于老旧,极易被识别为非浏览器请求,替换为当前主流浏览器的UA:

'User-Agent' = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'

2. 添加Referer请求头

Investing.com会验证请求的来源页面,必须添加对应数据页面的Referer:

'Referer' = 'https://www.investing.com/indices/stoxx-50-volatility-historical-data'

3. 先获取页面会话Cookie再发起请求

部分网站需要先访问主页面获取有效会话Cookie,才能调用数据接口:

# 先访问目标数据页面,获取会话信息
$mainPageUri = 'https://www.investing.com/indices/stoxx-50-volatility-historical-data'
$mainSession = Invoke-WebRequest -Uri $mainPageUri -Headers $updatedHeaders

# 携带会话发起POST请求
$res = Invoke-WebRequest -Method POST -Headers $updatedHeaders -Uri $uri -Body $params -ContentType application/x-www-form-urlencoded -WebSession $mainSession.Session

完整修改后的脚本示例

$uri = 'https://www.investing.com/instruments/HistoricalDataAjax'
$mainPageUri = 'https://www.investing.com/indices/stoxx-50-volatility-historical-data'

$params = @{
    'curr_id'= '951481'
    'smlID'= '2081817'
    'header'= 'STOXX 50 Volatility VSTOXX EUR Historical Data'
    'st_date'= '04/13/2021'
    'end_date'= '04/13/2022'
    'interval_sec'= 'Daily'
    'sort_col'= 'date'
    'sort_ord'= 'DESC'
    'action'= 'historical_data'
}

$headers = @{
    'User-Agent' = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
    'X-Requested-With'= 'XMLHttpRequest'
    'Accept'= 'text/html'
    'Accept-Encoding'= 'gzip, deflate'
    'Referer' = $mainPageUri
}

# 获取主页面会话
$mainSession = Invoke-WebRequest -Uri $mainPageUri -Headers $headers

# 携带会话请求数据接口
$res = Invoke-WebRequest -Method POST -Headers $headers -Uri $uri -Body $params -ContentType application/x-www-form-urlencoded -WebSession $mainSession.Session

额外注意事项

  • 检查curr_id和smlID是否有效:可通过浏览器F12开发者工具,查看当前页面调用接口的真实参数值,避免网站更新参数导致请求失效
  • 控制请求频率:短时间内频繁请求可能触发IP封禁
  • 复制真实请求头:如果仍失败,可在浏览器中捕获接口请求的所有头信息,直接复制到脚本的headers集合中

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 17:12:30