沃尔玛是美国第二大电子商务平台,对于价格比较工具、零售分析和竞争情报至关重要。直接抢夺沃尔玛被积极封杀。 Scavio API 提供了一个稳定的沃尔玛搜索端点,可返回产品列表,包括标题、价格、评级、评论计数、卖家信息和可用性。本教程演示如何搜索产品、提取定价数据以及构建简单的产品列表。
前置条件
- Python 3.8 或更高版本
- 请求已安装库
- Scavio API 密钥
- 熟悉 REST API 调用和 JSON
操作指南
步骤 1: 发送沃尔玛产品搜索
使用 walmart 平台和您的搜索查询发布到 Scavio 端点。响应包含产品结果列表。
Python
def search_walmart(query: str) -> dict:
response = requests.post(
"https://api.scavio.dev/api/v1/search",
headers={"x-api-key": API_KEY},
json={"platform": "walmart", "query": query}
)
response.raise_for_status()
return response.json()步骤 2: 提取产品列表
产品密钥包含产品对象的列表。每个都有标题、价格、评级、评论和可用性。
Python
data = search_walmart("wireless headphones")
products = data.get("products", [])
print(f"Found {len(products)} products")步骤 3: 按价格排序
解析价格字段并对产品进行排序以找到最便宜的选项。价格可能包含货币符号,因此在转换之前将其去掉。
Python
def parse_price(p: str) -> float:
return float(p.replace("$", "").replace(",", "")) if p else float("inf")
sorted_products = sorted(products, key=lambda x: parse_price(x.get("price", "")))
for p in sorted_products[:5]:
print(f"{p['title'][:50]} — {p['price']}")步骤 4: 将结果保存为 JSON
将产品数据保存到 JSON 文件中以供下游处理或比较。
Python
import json
with open("walmart_results.json", "w") as f:
json.dump(products, f, indent=2)
print("Saved to walmart_results.json")Python 示例
Python
import os
import json
import requests
API_KEY = os.environ.get("SCAVIO_API_KEY", "your_scavio_api_key")
ENDPOINT = "https://api.scavio.dev/api/v1/search"
def search_walmart(query: str) -> list[dict]:
r = requests.post(ENDPOINT, headers={"x-api-key": API_KEY},
json={"platform": "walmart", "query": query})
r.raise_for_status()
return r.json().get("products", [])
def cheapest(products: list[dict], n: int = 5) -> list[dict]:
def price(p):
s = p.get("price", "")
return float(s.replace("$", "").replace(",", "")) if s else float("inf")
return sorted(products, key=price)[:n]
if __name__ == "__main__":
products = search_walmart("wireless headphones")
for p in cheapest(products):
print(f"{p['title'][:50]} — {p.get('price', 'N/A')} ({p.get('rating', 'N/A')} stars)")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 searchWalmart(query) {
const res = await fetch(ENDPOINT, {
method: "POST",
headers: { "x-api-key": API_KEY, "Content-Type": "application/json" },
body: JSON.stringify({ platform: "walmart", query })
});
const data = await res.json();
return data.products || [];
}
function parsePrice(str) {
return str ? parseFloat(str.replace(/[$,]/g, "")) : Infinity;
}
async function main() {
const products = await searchWalmart("wireless headphones");
const sorted = products.sort((a, b) => parsePrice(a.price) - parsePrice(b.price));
sorted.slice(0, 5).forEach(p => console.log(`${p.title.slice(0, 50)} — ${p.price}`));
}
main().catch(console.error);预期输出
JSON
{
"products": [
{
"title": "Sony WH-1000XM5 Wireless Headphones",
"price": "$279.00",
"rating": "4.8",
"reviews_count": 12847,
"availability": "In Stock",
"seller": "Walmart.com",
"url": "https://walmart.com/ip/sony-wh1000xm5/..."
}
]
}