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

使用BS4抓取达美乐加拿大促销页面无结果,求解决方案

解决达美乐加拿大官网促销信息抓取无结果的问题

嘿,我来帮你排查下为什么用requests和BeautifulSoup抓不到促销信息~

可能的问题原因

  • 动态内容渲染:达美乐加拿大官网的促销活动大概率是通过JavaScript动态加载的。requests.get()只能获取到服务器返回的初始HTML,而JS后续渲染的内容不会包含在这个响应里,所以你找不到目标class的元素。
  • 反爬机制拦截:网站可能检测到你的请求来自脚本而非真实浏览器,返回了简化版或空的页面内容,导致BeautifulSoup无法解析到目标元素。

解决方案

方案1:模拟浏览器请求头

先试试给requests添加浏览器的User-Agent头,伪装成真实浏览器请求,看看能不能拿到完整内容:

import requests
from bs4 import BeautifulSoup

url = "https://www.dominos.ca/"
# 模拟Chrome浏览器的请求头
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",
    "Accept-Language": "en-CA,en;q=0.9"
}

response = requests.get(url, headers=headers)
# 可以先打印响应的文本,看看是否包含促销相关内容
# print(response.text)

soup = BeautifulSoup(response.content, "html.parser")
promo_titles = soup.find_all("span", class_="promo__title__emphasis")

if promo_titles:
    for title in promo_titles:
        promo_name = title.text.strip()
        print(f"促销名称:{promo_name}")
        # 查找对应的价格元素(需要根据页面结构调整,这里假设价格在同级的promo__price类里)
        promo_price = title.find_next("span", class_="promo__price")
        if promo_price:
            print(f"促销价格:{promo_price.text.strip()}\n")
else:
    print("未找到促销元素,大概率是内容由JS动态加载,需要用浏览器模拟工具")

方案2:使用浏览器模拟工具(推荐)

如果上面的方法还是不行,说明内容是完全动态渲染的,需要用Selenium或Playwright这类能模拟浏览器加载JS的工具。下面是Selenium的示例:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup

# 配置Chrome无头模式(不显示浏览器窗口)
chrome_options = Options()
chrome_options.add_argument("--headless=new")
chrome_options.add_argument("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")

# 初始化浏览器驱动
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.dominos.ca/")

# 显式等待促销元素加载完成(最多等10秒)
try:
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CLASS_NAME, "promo__title__emphasis"))
    )
except Exception as e:
    print("等待促销元素加载超时:", e)
    driver.quit()
    exit()

# 获取渲染后的页面源码
page_source = driver.page_source
soup = BeautifulSoup(page_source, "html.parser")

# 提取促销信息
promo_items = soup.find_all("div", class_="promo__item")
for item in promo_items:
    title_elem = item.find("span", class_="promo__title__emphasis")
    price_elem = item.find("span", class_="promo__price")
    if title_elem and price_elem:
        print(f"促销名称:{title_elem.text.strip()}")
        print(f"促销价格:{price_elem.text.strip()}\n")

# 关闭浏览器
driver.quit()

注意事项

  • 请遵守网站的robots.txt规则,不要频繁发送请求,避免给服务器造成压力。
  • 如果网站有更严格的反爬机制(比如验证码),可能需要进一步调整策略,比如使用代理或验证码识别工具。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:46:50