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

Python解析scrollable HTML元素:Azure Marketplace定价表爬取问题

解决Azure Marketplace定价表格动态加载爬取问题

问题原因

你遇到的情况是因为Plans + Pricing标签页的表格内容是通过JavaScript动态渲染的,BeautifulSoup只能解析页面初始加载的静态HTML,此时表格还未加载完成,所以只会返回"loading..."文本。

解决方案

方法1:使用Selenium模拟浏览器渲染

Selenium可以模拟真实浏览器的行为,等待JavaScript执行完成后再抓取渲染后的内容。

  1. 安装依赖:
pip install selenium
  1. 下载对应浏览器的驱动(比如Chrome的ChromeDriver),确保驱动版本和浏览器版本匹配。
  2. 示例代码:
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浏览器驱动,替换为你的驱动路径
driver = webdriver.Chrome(executable_path="path/to/your/chromedriver")
driver.get("https://azuremarketplace.microsoft.com/en-us/marketplace/apps/esri.arcgis-m365?tab=PlansAndPrice")

# 等待表格加载完成,超时时间10秒
wait = WebDriverWait(driver, 10)
table = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "pricing-table")))

# 提取表格行数据
rows = table.find_elements(By.TAG_NAME, "tr")
for row in rows:
    cols = row.find_elements(By.TAG_NAME, "td")
    print([col.text.strip() for col in cols if col.text.strip()])

# 关闭浏览器
driver.quit()

方法2:直接调用后端API接口(更高效)

动态加载的数据通常来自后端API,你可以通过浏览器开发者工具找到对应的接口,直接请求获取结构化的定价数据:

  1. 打开浏览器开发者工具(F12),切换到Network面板,刷新页面。
  2. 在XHR/Fetch请求列表中,筛选包含"plan""price"等关键词的接口。
  3. 复制目标接口的URL和请求头信息,用requests库直接请求:
import requests

# 替换为你找到的实际API接口URL
api_url = "https://marketplaceapi.microsoft.com/api/v1/products/esri.arcgis-m365/plans?api-version=2018-08-01"
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"
}

response = requests.get(api_url, headers=headers)
if response.status_code == 200:
    pricing_data = response.json()
    # 按需提取方案名称、定价等字段
    for plan in pricing_data.get("plans", []):
        print(f"方案名称: {plan['name']}")
        print(f"定价: {plan['pricingDetails']['price']}")

注:API接口可能有参数或鉴权限制,需要确保请求头、参数与浏览器请求完全一致,否则可能返回错误数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 21:10:30