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

使用Python BS4解析阿迪达斯商品页面无法获取颜色和尺码信息,且请求存在10秒延迟阻塞问题

使用Python BS4解析阿迪达斯商品页面无法获取颜色和尺码信息,且请求存在10秒延迟阻塞问题

看起来你遇到了阿迪达斯反爬机制和动态内容渲染的双重问题,我来帮你拆解解决思路和具体方案:

一、先解决10秒延迟阻塞的反爬问题

阿迪达斯网站有反爬检测,当识别到非浏览器的请求时,会故意延迟响应或者返回不完整内容,你可以从这几个方面优化:

  • 完善请求头(Headers):模拟真实浏览器的请求头,除了基础的User-Agent,还要补充Accept、Accept-Language、Referer等字段,让请求更贴近真实用户行为。示例如下:
headers = {
    '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',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Language': 'en-US,en;q=0.5',
    'Referer': 'https://www.adidas.co.uk/',
    'DNT': '1',
    'Connection': 'keep-alive',
    'Upgrade-Insecure-Requests': '1'
}
  • 使用Session保持会话:用requests.Session()发送请求,它会自动维护Cookie,避免每次请求都被视为新用户,降低被反爬拦截的概率:
import requests
from bs4 import BeautifulSoup

session = requests.Session()
session.headers.update(headers)
response = session.get("https://www.adidas.co.uk/gazelle-shoes/BB5478.html")
  • 添加合理的请求间隔:不要频繁发送请求,每次请求后加上1-3秒的随机延迟,避免触发频率限制。

二、解决颜色和尺码无法获取的问题

颜色、尺码这类信息通常是通过JavaScript动态渲染到页面上的,静态requests.get只能拿到页面初始HTML,无法获取JS加载后的内容,有两种可行的解决办法:

方法1:使用自动化工具模拟浏览器渲染(推荐)

用Selenium或者Playwright这类工具,模拟真实浏览器打开页面,等待JS加载完成后再提取内容。以Selenium为例:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 初始化Chrome浏览器(需提前配置对应版本的chromedriver)
driver = webdriver.Chrome()
driver.get("https://www.adidas.co.uk/gazelle-shoes/BB5478.html")

try:
    # 等待颜色选项加载完成
    color_elements = WebDriverWait(driver, 10).until(
        EC.presence_of_all_elements_located((By.CLASS_NAME, "gl-label"))
    )
    print("可用颜色:")
    for color in color_elements:
        print(color.text.strip())
    
    # 等待尺码选项加载完成
    size_elements = WebDriverWait(driver, 10).until(
        EC.presence_of_all_elements_located((By.CLASS_NAME, "size-selector__list-item"))
    )
    print("\n可用尺码:")
    for size in size_elements:
        if not size.get_attribute("disabled"):
            print(size.text.strip())
finally:
    driver.quit()

方法2:直接调用商品数据API

打开浏览器开发者工具(F12),切换到Network标签页刷新页面,查找XHR/fetch请求,通常能找到阿迪达斯用来获取商品详细信息的API接口,直接请求该接口就能拿到结构化的颜色、尺码数据,这种方法比模拟浏览器更高效。示例如下:

import requests

headers = {
    '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',
    'Accept': 'application/json',
    'Referer': 'https://www.adidas.co.uk/gazelle-shoes/BB5478.html'
}
api_url = "https://www.adidas.co.uk/api/products/BB5478?sitePath=uk"
response = session.get(api_url, headers=headers)

if response.status_code == 200:
    product_data = response.json()
    # 提取颜色信息
    colors = product_data['variation_list']
    print("颜色信息:")
    for color in colors:
        print(color['title'])
    # 提取尺码信息
    sizes = product_data['size_list']
    print("\n尺码信息:")
    for size in sizes:
        if size['availability'] == 'IN_STOCK':
            print(size['title'])

备注:内容来源于stack exchange,提问作者IAM VGENIE

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.20 08:18:07