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

求助:无法用Google Sheets/Excel抓取指定网页截图内的信息

抓取加拿大航空航班状态信息的可行方案

一、Google Sheets 解决方案(用Google Apps Script)

Google Sheets自带的IMPORTDATA/IMPORTHTML只能抓取静态HTML,而目标页面是动态渲染的,所以用脚本模拟浏览器请求:

  • 打开你的Google Sheet,点击「扩展程序」→「Apps Script」
  • 替换默认代码为以下脚本:
function getFlightStatus() {
  const url = "https://www.aircanada.com/ca/en/aco/home/fly/flight-information/flight-status-results.html#/flight-status-results?method=byfn&date=01-15-2023&fn=887";
  // 模拟浏览器请求头,避免被拦截
  const options = {
    headers: {
      "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
    }
  };
  const response = UrlFetchApp.fetch(url, options);
  const html = response.getContentText();
  
  // 提取实际起飞时间(根据页面结构调整正则,若页面更新需修改)
  const actualDepartureMatch = html.match(/Actual Departure:<\/span>\s*<span[^>]*>(\d{1,2}:\d{2} [AP]M)/);
  const actualDeparture = actualDepartureMatch ? actualDepartureMatch[1] : "未找到";
  
  // 提取实际到达时间
  const actualArrivalMatch = html.match(/Actual Arrival:<\/span>\s*<span[^>]*>(\d{1,2}:\d{2} [AP]M)/);
  const actualArrival = actualArrivalMatch ? actualArrivalMatch[1] : "未找到";
  
  // 写入Sheet单元格
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  sheet.getRange("A1").setValue("实际起飞时间");
  sheet.getRange("B1").setValue(actualDeparture);
  sheet.getRange("A2").setValue("实际到达时间");
  sheet.getRange("B2").setValue(actualArrival);
}
  • 点击「运行」,首次运行需授权权限;若页面结构变动,调整正则表达式即可

二、Excel 解决方案(两种路径)

路径1:Power Query 抓API接口

  • 打开目标航班页面,按F12打开开发者工具→「网络」标签,刷新页面后筛选「XHR/Fetch」,找到返回航班数据的API(一般是带flightstatus的接口)
  • 复制API的请求URL,打开Excel→「数据」→「获取数据」→「从Web」,粘贴URL后加载数据,在Power Query编辑器中提取所需字段

路径2:VBA + Selenium 模拟浏览器

  • 先安装Selenium Basic,打开Excel按Alt+F11进入VBA编辑器,引用「Selenium Type Library」
  • 粘贴以下代码:
Sub GetFlightStatus()
    Dim driver As New ChromeDriver
    driver.Get "https://www.aircanada.com/ca/en/aco/home/fly/flight-information/flight-status-results.html#/flight-status-results?method=byfn&date=01-15-2023&fn=887"
    driver.Wait 5000 '等待页面加载完成
    
    '提取实际起飞时间(根据页面元素路径调整)
    Dim actualDeparture As String
    actualDeparture = driver.FindElementByXPath("//span[contains(text(),'Actual Departure')]/following-sibling::span").Text
    
    '提取实际到达时间
    Dim actualArrival As String
    actualArrival = driver.FindElementByXPath("//span[contains(text(),'Actual Arrival')]/following-sibling::span").Text
    
    '写入Excel单元格
    Range("A1").Value = "实际起飞时间"
    Range("B1").Value = actualDeparture
    Range("A2").Value = "实际到达时间"
    Range("B2").Value = actualArrival
    
    driver.Quit
End Sub
  • 运行宏即可获取数据

三、Python 脚本方案(灵活适配批量抓取)

用Selenium模拟浏览器,适合批量处理多个航班:

  • 先安装依赖:pip install selenium,并下载对应浏览器的驱动(如ChromeDriver)
  • 运行以下代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome()
driver.get("https://www.aircanada.com/ca/en/aco/home/fly/flight-information/flight-status-results.html#/flight-status-results?method=byfn&date=01-15-2023&fn=887")
time.sleep(5)  # 等待页面加载

# 提取目标信息
actual_departure = driver.find_element(By.XPATH, "//span[contains(text(),'Actual Departure')]/following-sibling::span").text
actual_arrival = driver.find_element(By.XPATH, "//span[contains(text(),'Actual Arrival')]/following-sibling::span").text

print(f"实际起飞时间: {actual_departure}")
print(f"实际到达时间: {actual_arrival}")

driver.quit()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 16:51:14