深度分析大麦网API接口,用Python脚本实现

简介: 大麦网为国内领先演出票务平台,提供演唱会、话剧、体育赛事等票务服务。本文基于抓包分析其非官方接口,并提供Python调用方案,涵盖演出列表查询、详情获取及城市列表获取。需注意非官方接口存在稳定性风险,使用时应遵守平台规则,控制请求频率,防范封禁与法律风险。适用于个人学习、演出信息监控等场景。

大麦网是国内领先的演出票务平台,涵盖演唱会、话剧、体育赛事等票务服务。由于大麦网未正式开放公共 API,以下分析基于其移动端 / 网页端的非官方接口(通过抓包分析),并提供 Python 调用方案。需注意:非官方 API 可能存在稳定性问题,且使用需遵守平台规则。

一、大麦网接口核心特性分析

1. 接口体系与功能域

通过抓包分析,大麦网核心接口可分为以下几类:

  • 首页推荐:获取热门演出、分类推荐;
  • 演出列表:按城市、品类、时间筛选演出;
  • 演出详情:获取演出时间、场馆、票价、座位图;
  • 购票相关:库存查询、下单、支付;
  • 用户中心:订单查询、收货地址。

    2. 认证与请求规范

  • 匿名接口:首页推荐、演出列表等公开信息接口无需登录,仅需标准请求头;
  • 登录态接口:购票、订单查询等需携带登录 Cookie(关键 Cookie 为damai.cnSESSIONuserId);
  • 请求头:必需User-Agent(模拟移动端 / PC 端)、Referer(防盗链);
  • 参数加密:部分接口参数经过简单加密(如时间戳 + 固定密钥 MD5),但公开信息接口参数多为明文。

    3. 典型接口参数与响应

    演出列表接口为例:
  • 请求 URL
  • 方法:GET
  • 核心参数
    • city:城市 ID(如北京101010100);
    • ctl:品类(演唱会1、话剧2、体育5等);
    • page:页码;
    • ts:时间戳(毫秒级)。
  • 响应格式:JSON,包含totalCount(总数)、pageData(演出列表,含 ID、名称、价格等)。

    二、Python 脚本实现:大麦网接口调用框架

    以下实现大麦网公开接口的调用,包括演出列表查询、演出详情获取,并处理基本的请求头和参数构造。
    import requests
    import json
    import time
    import logging
    from typing import Dict, List, Optional
    from requests.exceptions import RequestException

配置日志

logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)

class DamaiAPI:
def init(self, city_id: str = "101010100"):
"""
初始化大麦网API客户端
:param city_id: 城市ID(默认北京:101010100,其他城市需查询对应ID)
"""
self.city_id = city_id
self.headers = {
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Damai/7.2.0",
"Referer": "https://m.damai.cn/",
"Origin": "https://m.damai.cn",
"Accept": "application/json, text/plain, /"
}

    # 会话保持(处理Cookie)
    self.session = requests.Session()
    self.session.headers.update(self.headers)

def _get_timestamp(self) -> int:
    """生成毫秒级时间戳"""
    return int(time.time() * 1000)

def search_performances(self, keyword: str = "", category: int = 0, page: int = 1) -> Optional[List[Dict]]:
    """
    搜索演出列表
    :param keyword: 搜索关键词(如"周杰伦")
    :param category: 品类(1:演唱会,2:话剧,5:体育,0:全部)
    :param page: 页码
    :return: 演出列表(含ID、名称、价格等)
    """
    url = "https://search.damai.cn/searchajax.html"
    params = {
        "city": self.city_id,
        "keyword": keyword,
        "ctl": category,
        "page": page,
        "ts": self._get_timestamp(),
        "stype": 0,
        "order": 1  # 1:热门排序,2:时间排序
    }

    try:
        response = self.session.get(url, params=params, timeout=10)
        response.raise_for_status()
        result = response.json()

        # 解析演出列表
        if result.get("status") == 1:
            performances = result.get("pageData", {}).get("resultData", [])
            logging.info(f"搜索到{len(performances)}个演出,关键词:{keyword},页码:{page}")
            return [
                {
                    "id": item.get("id"),
                    "name": item.get("name"),
                    "category": item.get("categoryName"),
                    "time": item.get("showTime"),
                    "venue": item.get("venueName"),
                    "price": item.get("priceStr"),
                    "status": item.get("statusName")  # 售票状态:预售/在售/售罄
                } for item in performances
            ]
        else:
            logging.error(f"搜索失败:{result.get('msg')}")
            return None

    except RequestException as e:
        logging.error(f"搜索请求异常:{str(e)}")
        return None

def get_performance_detail(self, performance_id: str) -> Optional[Dict]:
    """
    获取演出详情(时间、场馆、票价等)
    :param performance_id: 演出ID(从搜索接口获取)
    :return: 演出详情字典
    """
    url = f"https://detail.damai.cn/item.htm?id={performance_id}"
    # 详情页接口实际通过HTML渲染,需解析页面中的JSON数据
    try:
        response = self.session.get(url, timeout=10)
        response.raise_for_status()
        html = response.text

        # 提取页面中的JSON数据(大麦网详情页数据嵌在window.__INITIAL_STATE__中)
        start = html.find("window.__INITIAL_STATE__ = ") + len("window.__INITIAL_STATE__ = ")
        end = html.find(";</script>", start)
        if start == -1 or end == -1:
            logging.error("无法提取演出详情数据")
            return None

        detail_json = json.loads(html[start:end])
        item_info = detail_json.get("itemDetail", {}).get("item", {})
        sku_list = detail_json.get("skuList", {}).get("skuList", [])  # 票价信息

        # 解析核心信息
        return {
            "id": performance_id,
            "name": item_info.get("name"),
            "poster": item_info.get("verticalPic"),  # 海报图
            "time": item_info.get("showTime"),
            "venue": item_info.get("venueName"),
            "address": item_info.get("venueAddress"),
            "price_list": [
                {
                    "price": sku.get("price"),
                    "desc": sku.get("skuName"),  # 票价描述(如"内场VIP")
                    "stock": sku.get("stockStatusName")  # 库存状态
                } for sku in sku_list
            ],
            "description": item_info.get("detailDesc")  # 演出简介
        }

    except RequestException as e:
        logging.error(f"详情请求异常:{str(e)}")
        return None
    except json.JSONDecodeError:
        logging.error("解析演出详情JSON失败")
        return None

def get_city_list(self) -> Optional[List[Dict]]:
    """获取大麦网支持的城市列表(含城市ID)"""
    url = "https://city.damai.cn/js/cityjson.js"
    try:
        response = self.session.get(url, timeout=10)
        response.raise_for_status()
        # 解析城市数据(格式为var cityList = {...};)
        start = response.text.find("=") + 1
        end = response.text.rfind(";")
        city_data = json.loads(response.text[start:end])

        # 提取城市ID和名称
        return [
            {"id": city.get("id"), "name": city.get("name")}
            for city in city_data.get("cityList", [])
        ]
    except Exception as e:
        logging.error(f"获取城市列表失败:{str(e)}")
        return None

示例调用

if name == "main":

# 初始化客户端(默认北京,可通过get_city_list()获取其他城市ID)
damai = DamaiAPI(city_id="101010100")  # 北京

# 1. 搜索演出(示例:搜索"演唱会")
performances = damai.search_performances(keyword="演唱会", category=1, page=1)
if performances:
    print("演出列表:")
    for perf in performances[:3]:  # 打印前3个
        print(f"[{perf['id']}] {perf['name']} | {perf['time']} | {perf['venue']} | {perf['price']} | {perf['status']}")

# 2. 获取演出详情(使用搜索结果中的第一个演出ID)
if performances:
    first_perf_id = performances[0]["id"]
    detail = damai.get_performance_detail(first_perf_id)
    if detail:
        print(f"\n演出详情:{detail['name']}")
        print(f"时间:{detail['time']}")
        print(f"场馆:{detail['venue']}({detail['address']})")
        print("票价信息:")
        for price in detail["price_list"]:
            print(f"- {price['desc']}:{price['price']}元({price['stock']})")

三、关键技术点解析

1. 接口特点与反爬应对

  • 动态参数:部分接口需携带时间戳(ts),需实时生成;
  • HTML 内嵌数据:演出详情数据嵌在页面的window.__INITIAL_STATE__中,需从 HTML 中提取 JSON;
  • 请求头模拟:需设置真实的User-Agent(推荐移动端 UA)和Referer,否则可能返回 403;
  • IP 限制:频繁请求可能触发 IP 封禁,建议添加随机间隔(如time.sleep(1-3秒))。

    2. 登录态处理(可选)

    如需调用购票、订单等需登录的接口,需:
  1. 手动登录大麦网,从浏览器 Cookie 中提取SESSIONuserId
  2. 将 Cookie 添加到会话中:
    self.session.cookies.set("SESSION", "your_session_value")
    self.session.cookies.set("userId", "your_user_id")
    
  3. 注意 Cookie 有效期(通常为 24 小时),需定期更新。

    3. 城市 ID 映射

    大麦网城市 ID 为固定编码(如北京101010100、上海101020100),可通过get_city_list()方法获取完整列表,或手动查询对应城市 ID。

    四、风险与注意事项

  4. 合规性:非官方 API 可能违反大麦网用户协议,用于商业用途需谨慎;
  5. 稳定性:接口地址、参数或响应格式可能随时变更,需定期维护;
  6. 反爬限制:高频请求可能导致 IP 封禁,建议控制频率并使用代理池;
  7. 法律风险:未经授权的大规模爬取可能涉及法律责任,建议仅用于个人学习。
    通过上述框架,可实现大麦网公开演出信息的查询,适用于个人兴趣的演出监控、信息聚合等场景。实际使用中需根据接口变化及时调整参数和解析逻辑。
相关文章
|
8天前
|
缓存 API 网络架构
淘宝item_search_similar - 搜索相似的商品API接口,用python返回数据
淘宝联盟开放平台中,可通过“物料优选接口”(taobao.tbk.dg.optimus.material)实现“搜索相似商品”功能。该接口支持根据商品 ID 获取相似推荐商品,并返回商品信息、价格、优惠等数据,适用于商品推荐、比价等场景。本文提供基于 Python 的实现示例,包含接口调用、数据解析及结果展示。使用时需配置淘宝联盟的 appkey、appsecret 和 adzone_id,并注意接口调用频率限制和使用规范。
|
8天前
|
JSON API 数据安全/隐私保护
深度分析淘宝卖家订单详情API接口,用json返回数据
淘宝卖家订单详情API(taobao.trade.fullinfo.get)是淘宝开放平台提供的重要接口,用于获取单个订单的完整信息,包括订单状态、买家信息、商品明细、支付与物流信息等,支撑订单管理、ERP对接及售后处理。需通过appkey、appsecret和session认证,并遵守调用频率与数据权限限制。本文详解其使用方法并附Python调用示例。
|
10天前
|
数据采集 JSON 监控
巧用快手电商 API,精准分析快手商品销售趋势
快手电商API助力商家精准分析销售趋势,通过实时数据获取、趋势识别与模型构建,优化营销策略,提升市场竞争力。
41 1
|
12天前
|
数据采集 存储 JSON
地区电影市场分析:用Python爬虫抓取猫眼/灯塔专业版各地区票房
地区电影市场分析:用Python爬虫抓取猫眼/灯塔专业版各地区票房
|
12天前
|
JSON API 开发者
深度分析阿里妈妈API接口,用Python脚本实现
阿里妈妈是阿里巴巴旗下营销平台,提供淘宝联盟、直通车等服务,支持推广位管理、商品查询等API功能。本文详解其API调用方法,重点实现商品推广信息(佣金、优惠券)获取,并提供Python实现方案。
|
10天前
|
API Python
Python采集淘宝店铺所有商品API接口指南
淘宝没有公开的官方API供采集店铺商品数据,但可以通过以下几种方法获取商品信息。需要注意的是,淘宝有严格的反爬机制,直接采集可能违反其服务条款。
|
11天前
|
数据采集 数据可视化 API
驱动业务决策:基于Python的App用户行为分析与可视化方案
驱动业务决策:基于Python的App用户行为分析与可视化方案
|
11天前
|
API 数据安全/隐私保护 开发者
深度分析苏宁API接口,用Python脚本实现
深度分析苏宁API接口,用Python脚本实现
|
开发框架 jenkins 持续交付
跨平台API对接(Python)的使用
跨平台API对接(Python)的使用
|
开发框架 jenkins 持续交付
跨平台API对接(Python)的使用
![](https://ceshiren.com/uploads/default/original/3X/3/a/3a86a19fb6dbb3f346088c7323fa31227d08207b.png) ## Python-Jenkins Python-Jenkins 通过 HTTP 方式运行 Jenkins job 。 Python-Jenkins 官网:https://pypi.py

推荐镜像

更多