如何通过自定义App Script实现需密码网站数据每小时自动导入谷歌表格
解决方法:模拟登录+定时更新
原代码里的IMPORTHTML没法处理需要登录的页面——它是匿名请求,不会携带任何登录凭证,所以直接用它拿不到需要权限的订单数据。得换用UrlFetchApp模拟登录流程,再解析页面内容,最后设置定时触发器自动更新。
步骤1:模拟登录获取会话凭证
首先得用浏览器开发者工具的Network面板抓包分析Bybit的登录请求,找到登录接口、需要提交的参数(比如账号、密码、CSRF token、验证码相关参数等)。下面是通用的模拟登录框架,你需要根据实际抓包结果调整参数:
function getLoginCookie() { // 1. 先访问登录页面获取CSRF token(如果页面要求的话) const loginPageUrl = "https://www.bybit.com/user/login"; const loginPageResponse = UrlFetchApp.fetch(loginPageUrl); const htmlContent = loginPageResponse.getContentText(); // 用正则提取CSRF token,需根据实际页面结构调整正则表达式 const csrfTokenMatch = htmlContent.match(/name="csrf-token" content="([^"]+)"/); const csrfToken = csrfTokenMatch ? csrfTokenMatch[1] : ""; // 2. 构造登录请求参数 const loginPayload = { username: PropertiesService.getScriptProperties().getProperty("BYBIT_USERNAME"), password: PropertiesService.getScriptProperties().getProperty("BYBIT_PASSWORD"), csrfToken: csrfToken, // 其他可能需要的参数(如验证码、rememberMe等),根据抓包结果补充 }; // 3. 发送登录请求,获取会话Cookie const loginOptions = { method: "post", payload: loginPayload, followRedirects: true, headers: { "Content-Type": "application/x-www-form-urlencoded", "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" }, muteHttpExceptions: true // 允许查看错误响应内容 }; const loginResponse = UrlFetchApp.fetch("https://www.bybit.com/api/user/login", loginOptions); // 从响应头提取Cookie const cookies = loginResponse.getAllHeaders()["Set-Cookie"]; return cookies ? cookies.join("; ") : ""; }
步骤2:请求目标页面并解析表格数据
拿到登录Cookie后,用它请求订单页面,再解析HTML表格内容写入谷歌表格:
function fetchAndUpdateOrders() { const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // 清空表格原有数据 sheet.clearContents(); // 获取登录会话Cookie const sessionCookie = getLoginCookie(); if (!sessionCookie) { sheet.getRange("A1").setValue("登录失败,请检查账号密码或请求参数"); return; } // 请求订单页面 const orderPageUrl = "https://www.bybit.com/user/assets/order/derivatives/uniform-usdt/all-orders"; const requestOptions = { headers: { "Cookie": sessionCookie, "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" } }; const response = UrlFetchApp.fetch(orderPageUrl, requestOptions); const html = response.getContentText(); // 解析HTML表格(这里用XmlService,若页面HTML不规范可能需要预处理) try { const doc = XmlService.parse(html); const root = doc.getRootElement(); // 定位目标表格(根据页面实际结构调整筛选逻辑) const tables = root.getDescendants().filter(el => el.asElement() && el.asElement().getTagName() === "table"); const targetTable = tables[0]; // 假设第一个表格是目标订单表 // 提取表格行和单元格内容 const rows = targetTable.getDescendants().filter(el => el.asElement() && el.asElement().getTagName() === "tr"); const data = rows.map(row => { const cells = row.getDescendants().filter(el => el.asElement() && (el.asElement().getTagName() === "td" || el.asElement().getTagName() === "th")); return cells.map(cell => cell.asElement().getText().trim()); }); // 将解析后的数据写入表格 if (data.length > 0) { sheet.getRange(1, 1, data.length, data[0].length).setValues(data); } } catch (e) { sheet.getRange("A1").setValue("表格解析失败:" + e.message); } }
步骤3:设置每小时自动更新
- 打开Google App Script编辑器,点击左侧的「触发器」图标(时钟形状)。
- 点击「添加触发器」:
- 选择运行函数:
fetchAndUpdateOrders - 事件源选择:「时间驱动」
- 时间类型选择:「小时计时器」
- 间隔选择:「每小时」
- 选择运行函数:
- 保存触发器,脚本就会每小时自动执行一次。
关键注意事项
- 安全存储凭证:不要把账号密码硬编码在脚本里!点击编辑器菜单「项目设置」→「脚本属性」,添加
BYBIT_USERNAME和BYBIT_PASSWORD两个属性,代码里用PropertiesService读取,避免泄露。 - 适配Bybit接口变化:Bybit的登录逻辑、页面结构可能随时更新,上述代码只是基础框架,你需要自己抓包确认最新的登录参数和页面结构。
- 反爬规避:频繁请求可能触发Bybit的反爬机制,建议适当调整请求间隔,优先考虑使用Bybit官方API(比爬页面更稳定可靠)。
内容的提问来源于stack exchange,提问作者user145632789
相关产品推荐
相关产品推荐

