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

首次Web Scraping:如何提取酒店日历嵌套<span>中的价格?

提取酒店日历价格的实现方案

方法1:直接定位嵌套的<strong>元素

你已经通过document.querySelectorAll('[aria-label="mar 31 ott 2023"]')获取到目标日期节点集合,接下来遍历这些节点,直接抓取内部的<strong>元素就能拿到价格,完全不受innerText里日期文字的干扰:

// 获取所有目标日期节点
const dateNodes = document.querySelectorAll('[aria-label="mar 31 ott 2023"]');

// 遍历节点提取价格
dateNodes.forEach(node => {
  // 精准定位价格所在的strong元素
  const priceElement = node.querySelector('span > strong');
  if (priceElement) {
    const price = priceElement.textContent.trim();
    console.log('提取到的价格:', price); // 输出示例:999€
    // 这里可以把price传入你的Telegram Bot通知逻辑
  }
});

方法2:正则表达式匹配(适配结构变动场景)

如果后续页面结构有小调整,比如strong元素的层级变化,可以用正则从节点的innerText里匹配带€符号的价格,兼容性更强:

const dateNodes = document.querySelectorAll('[aria-label="mar 31 ott 2023"]');

dateNodes.forEach(node => {
  const fullText = node.innerText;
  // 匹配数字+€的格式,支持千分位分隔符(如1.299€)
  const priceMatch = fullText.match(/\d+(?:\.\d+)?€/);
  if (priceMatch) {
    const price = priceMatch[0];
    console.log('提取到的价格:', price);
  }
});

项目优化小提示

  • 定时任务:用node-schedule库可以轻松实现每天8:05自动触发查询逻辑
  • 反爬注意:添加合理的请求头、控制请求频率,避免被网站封禁
  • 通知格式:把价格和日期整理成清晰的消息,比如"2023年10月31日酒店价格:999€",再通过Telegram Bot API发送

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 14:36:08