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

如何通过API而非Puppeteer提取Azure估算详情并导入数据库

如何通过API提取Azure估算分享数据并导入数据库

方案一:利用Azure定价计算器的内部API(无需认证)

Azure定价计算器的分享链接包含唯一的估算ID,可通过官方内部API直接获取JSON格式的估算数据,无需解析XLSX或使用浏览器自动化工具。

  1. 提取估算ID
    从分享链接中截取id参数值,例如链接https://azure.microsoft.com/zh-cn/pricing/calculator/?id=xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx中的xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx就是估算ID,可通过正则表达式快速提取:

    import re
    share_url = "你的分享链接"
    estimate_id = re.search(r'id=([a-f0-9-]+)', share_url).group(1)
    
  2. 调用API获取数据
    发送POST请求到Azure定价计算器API,传入估算ID、货币和区域参数:

    import requests
    import json
    
    api_url = "https://azure.microsoft.com/api/v2/pricing/calculator?culture=zh-cn&discount=mosp&v=2023-01-01-preview"
    payload = {
        "id": estimate_id,
        "currency": "CNY",
        "locale": "zh-CN"
    }
    response = requests.post(api_url, json=payload)
    response.raise_for_status()
    estimate_data = response.json()
    
  3. 解析并导入数据库
    响应的JSON数据包含所有服务项的名称、数量、月度价格等信息,直接提取字段写入数据库即可:

    # 示例:提取核心字段(根据实际需求调整)
    for offer in estimate_data["offers"]:
        service_name = offer["name"]
        monthly_cost = offer["pricing"]["monthly"]
        quantity = offer["quantity"]
        # 此处编写数据库插入逻辑,例如使用SQLAlchemy或原生SQL
        # cursor.execute("INSERT INTO azure_estimates (service, cost, quantity) VALUES (%s, %s, %s)", (service_name, monthly_cost, quantity))
    

方案二:基于正式API的稳定方案(推荐长期使用)

如果担心内部API变更,可通过ARM模板+Azure零售价格API组合实现,全程使用微软官方正式API:

  1. 获取分享者的ARM模板
    让分享者在Azure定价计算器中导出ARM模板(界面右上角「导出」→「ARM模板」),提供给你。

  2. 解析ARM模板提取资源信息
    从ARM模板中读取资源类型、SKU、区域、数量等关键信息:

    with open("estimate_arm_template.json", "r") as f:
        arm_template = json.load(f)
    
    resources = arm_template.get("resources", [])
    for res in resources:
        resource_type = res["type"].split("/")[1]  # 提取服务名称
        sku_name = res["sku"]["name"]
        location = res["location"]
        quantity = res["properties"].get("capacity", 1)  # 默认数量为1
    
  3. 调用Azure零售价格API计算成本
    通过官方零售价格API获取对应资源的单价,再结合数量计算总成本:

    pricing_api_url = "https://prices.azure.com/api/retail/prices"
    params = {
        "api-version": "2023-01-01-preview",
        "$filter": f"serviceName eq '{resource_type}' and skuName eq '{sku_name}' and location eq '{location}'"
    }
    price_response = requests.get(pricing_api_url, params=params)
    price_response.raise_for_status()
    price_items = price_response.json()["Items"]
    
    if price_items:
        unit_price = price_items[0]["retailPrice"]
        monthly_cost = unit_price * quantity * 730  # 按每月730小时计算(按需调整)
        # 写入数据库逻辑
    

注意事项

  • 方案一的内部API无官方文档,但目前稳定可用,若需长期依赖优先选择方案二。
  • 使用正式API时,无需身份验证,但需确保过滤参数(服务名、SKU、区域)与资源完全匹配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 00:15:20