使用Google Apps Script调用UrlFetchApp遇Error 403的解决问询
解决Google Apps Script中UrlFetchApp请求Futbin返回403的问题
问题原因分析
你遇到的403错误是因为Futbin启用了Cloudflare反爬机制,你的请求被识别为自动化机器人请求。之前的请求头配置存在冗余(比如method和authority无需放在headers中,UrlFetchApp会自动处理),且缺少部分关键浏览器头信息,导致验证失败。
可行解决办法
1. 优化请求头配置
调整请求头,完全模拟真实浏览器的请求,去掉不必要的字段:
function fetchWithOptimizedHeaders() { var url = 'https://www.futbin.com/25/player/55430/robin-koch'; var headers = { "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", "accept-language": "en-US,en;q=0.9", "referer": "https://www.futbin.com/", "sec-fetch-dest": "document", "sec-fetch-mode": "navigate", "sec-fetch-site": "same-origin", "sec-fetch-user": "?1", "upgrade-insecure-requests": "1", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36" }; var options = { "method": "GET", "headers": headers, "muteHttpExceptions": true, "followRedirects": true }; try { var result = UrlFetchApp.fetch(url, options); var responseCode = result.getResponseCode(); if (responseCode === 200) { return result.getContentText(); } else { Logger.log("Response code: " + responseCode + ", Response: " + result.getContentText()); return null; } } catch (error) { Logger.log("Error: " + error); return null; } }
注意:user-agent要使用当前主流浏览器的最新版本,避免使用过时版本被识别。
2. 使用代理服务器
UrlFetchApp支持通过proxyUrl参数配置代理,你需要有一个可用的HTTP/HTTPS代理服务器(需支持HTTPS请求),配置示例:
function fetchWithProxy() { var url = 'https://www.futbin.com/25/player/55430/robin-koch'; var proxyUrl = "http://your-proxy-server:port"; // 替换为你的代理地址 // 如果代理需要认证,添加授权头 var proxyAuth = "Basic " + Utilities.base64Encode("proxy-username:proxy-password"); var headers = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36", "Proxy-Authorization": proxyAuth // 仅当代理需要认证时添加 }; var options = { "method": "GET", "headers": headers, "muteHttpExceptions": true, "proxyUrl": proxyUrl }; try { var result = UrlFetchApp.fetch(url, options); if (result.getResponseCode() === 200) { return result.getContentText(); } else { Logger.log("Proxy request failed with code: " + result.getResponseCode()); return null; } } catch (error) { Logger.log("Proxy error: " + error); return null; } }
说明:免费代理稳定性差,建议使用付费高匿代理,避免被Futbin识别并封禁。
3. 绕过Cloudflare JS验证(推荐)
由于UrlFetchApp无法执行JavaScript,遇到Cloudflare人机验证时直接请求会失败,可采用以下两种方式:
- 迁移到带JS渲染的云函数:将请求逻辑转移到Google Cloud Functions,使用Puppeteer等工具模拟浏览器渲染页面,再将结果返回给GAS。
- 使用第三方数据服务:查找提供Futbin球员数据的第三方API,直接调用API获取数据,避免网页爬取的反爬限制。
4. 控制请求频率
即使配置正确,频繁请求也会触发反爬机制,建议在重试逻辑中添加延迟:
function fetchWithRetryAndDelay() { var url = 'https://www.futbin.com/25/player/55430/robin-koch'; var maxAttempts = 4; var delayMs = 2000; // 每次重试间隔2秒 for (var attempt = 0; attempt < maxAttempts; attempt++) { try { var options = { "headers": { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36" }, "muteHttpExceptions": true }; var result = UrlFetchApp.fetch(url, options); if (result.getResponseCode() === 200) { return result.getContentText(); } } catch (error) { Logger.log("Attempt " + (attempt + 1) + " failed: " + error); } // 最后一次尝试不需要延迟 if (attempt < maxAttempts - 1) { Utilities.sleep(delayMs); } } return null; }
内容的提问来源于stack exchange,提问作者EagleEye
相关产品推荐
相关产品推荐

