Google 购物结果汇总了数千家零售商的产品列表,并在 Google 搜索结果中突出显示。它们是竞争性定价情报、商家发现和市场价格基准的宝贵来源。 Scavio API 在 SERP 响应的 shopping_results 字段中返回 Google 购物结果,包括产品标题、价格、商家、评级和图像 URL。本教程介绍如何提取和分析任何产品类别的 Google 购物数据。
前置条件
- Python 3.8 或更高版本
- 请求已安装库
- Scavio API 密钥
- 熟悉价格数据分析
操作指南
步骤 1: 获取 Google 购物结果
将“购物”上下文添加到您的查询中或直接查询产品名称。 Google 购物结果显示在 Scavio 响应中的 shopping_results 中。
Python
def get_shopping(product: str) -> list[dict]:
response = requests.post(
"https://api.scavio.dev/api/v1/search",
headers={"x-api-key": API_KEY},
json={"query": product, "country_code": "us"}
)
response.raise_for_status()
data = response.json()
return data.get("shopping_results", [])步骤 2: 按商家提取定价
建立一个字典,将商家名称映射到产品的列出价格。
Python
def prices_by_merchant(items: list[dict]) -> dict:
result = {}
for item in items:
merchant = item.get("source", "Unknown")
price = item.get("price", "N/A")
result[merchant] = price
return result步骤 3: 寻找最低价格
解析价格并确定提供最低标价的商家。
Python
def lowest_price(items: list[dict]) -> dict | None:
def parse(item):
p = item.get("price", "")
return float(p.replace("$", "").replace(",", "")) if p else float("inf")
return min(items, key=parse, default=None)步骤 4: 生成价格情报摘要
打印各商家价格范围的摘要并突出显示最优惠的价格。
Python
items = get_shopping("iPhone 16 Pro")
merchants = prices_by_merchant(items)
best = lowest_price(items)
print(f"Found {len(items)} listings")
print(f"Best price: {best.get('price')} from {best.get('source')}")
for m, p in list(merchants.items())[:5]:
print(f" {m}: {p}")Python 示例
Python
import os
import requests
API_KEY = os.environ.get("SCAVIO_API_KEY", "your_scavio_api_key")
ENDPOINT = "https://api.scavio.dev/api/v1/search"
def get_shopping(product: str) -> list[dict]:
r = requests.post(ENDPOINT, headers={"x-api-key": API_KEY},
json={"query": product, "country_code": "us"})
r.raise_for_status()
return r.json().get("shopping_results", [])
def lowest_price(items: list[dict]) -> dict | None:
def val(item):
p = item.get("price", "")
return float(p.replace("$", "").replace(",", "")) if p else float("inf")
return min(items, key=val, default=None)
if __name__ == "__main__":
items = get_shopping("Sony WH-1000XM5 headphones")
best = lowest_price(items)
print(f"{len(items)} shopping listings found")
if best:
print(f"Lowest: {best.get('price')} at {best.get('source')}")
for item in items[:5]:
print(f" {item.get('source', 'N/A')}: {item.get('price', 'N/A')}")JavaScript 示例
JavaScript
const API_KEY = process.env.SCAVIO_API_KEY || "your_scavio_api_key";
const ENDPOINT = "https://api.scavio.dev/api/v1/search";
async function getShopping(product) {
const res = await fetch(ENDPOINT, {
method: "POST",
headers: { "x-api-key": API_KEY, "Content-Type": "application/json" },
body: JSON.stringify({ query: product, country_code: "us" })
});
const data = await res.json();
return data.shopping_results || [];
}
async function main() {
const items = await getShopping("Sony WH-1000XM5 headphones");
const sorted = items.sort((a, b) => {
const pa = parseFloat((a.price || "").replace(/[$,]/g, "")) || Infinity;
const pb = parseFloat((b.price || "").replace(/[$,]/g, "")) || Infinity;
return pa - pb;
});
console.log(`${items.length} listings found`);
sorted.slice(0, 5).forEach(i => console.log(`${i.source}: ${i.price}`));
}
main().catch(console.error);预期输出
JSON
{
"shopping_results": [
{
"title": "Sony WH-1000XM5 Wireless Headphones",
"price": "$279.00",
"source": "Best Buy",
"rating": "4.8",
"reviews": 9842,
"link": "https://bestbuy.com/...",
"thumbnail": "https://cdn.bestbuy.com/..."
}
]
}