La surveillance des produits Amazon nécessite traditionnellement des scrapers web qui se cassent lorsque Amazon modifie son HTML. Ce tutoriel construit un pipeline de surveillance en utilisant la recherche de plateforme Amazon de l'API Scavio. Vous obtenez des données structurées sur les produits, y compris les prix, les évaluations et les classements, sans gérer de proxies ou de scrapers. Chaque vérification de produit coûte $0.005.
Prérequis
- Python 3.8+
- bibliothèque requests
- Une clé API Scavio provenant de scavio.dev
- ASINs ou mots-clés de produits Amazon à surveiller
Parcours
Étape 1: Rechercher des produits Amazon via l'API
Interroger les listes de produits Amazon via l'API de recherche au lieu de scraper.
import os, requests, json
from datetime import datetime
API_KEY = os.environ['SCAVIO_API_KEY']
SH = {'x-api-key': API_KEY, 'Content-Type': 'application/json'}
def search_amazon(query):
data = requests.post('https://api.scavio.dev/api/v1/search',
headers=SH, json={'query': query, 'platform': 'amazon', 'country_code': 'us'}, timeout=10).json()
products = []
for r in data.get('organic_results', []):
products.append({
'title': r.get('title', ''),
'link': r.get('link', ''),
'price': r.get('price', r.get('extracted_price', '')),
'rating': r.get('rating', ''),
'reviews': r.get('reviews', ''),
'position': r.get('position', 0),
'snippet': r.get('snippet', ''),
})
return products
# Monitor competitor products
MONITOR_QUERIES = [
'wireless bluetooth earbuds',
'mechanical keyboard gaming',
'portable phone charger',
]
for query in MONITOR_QUERIES:
products = search_amazon(query)
print(f'\n{query}: {len(products)} products')
for p in products[:3]:
print(f' #{p["position"]} {p["title"][:45]}')
print(f' Price: {p["price"]} | Rating: {p["rating"]} | Reviews: {p["reviews"]}')
print(f'\nCost: ${len(MONITOR_QUERIES) * 0.005:.3f}')Étape 2: Suivre les changements de prix et de classement
Stocker des instantanés quotidiens et détecter quand les prix ou les classements changent.
HISTORY_FILE = 'amazon_monitor_history.json'
def load_history():
try:
with open(HISTORY_FILE) as f:
return json.load(f)
except FileNotFoundError:
return {}
def save_snapshot(query, products):
history = load_history()
today = datetime.now().strftime('%Y-%m-%d')
if query not in history:
history[query] = []
history[query].append({
'date': today,
'products': [{'title': p['title'][:60], 'price': p['price'],
'position': p['position'], 'rating': p['rating']}
for p in products[:10]]
})
with open(HISTORY_FILE, 'w') as f:
json.dump(history, f, indent=2)
return history[query]
def detect_changes(query, products):
history = load_history()
snapshots = history.get(query, [])
if len(snapshots) < 1:
return []
prev = {p['title'][:60]: p for p in snapshots[-1]['products']}
changes = []
for p in products[:10]:
title_key = p['title'][:60]
if title_key in prev:
old = prev[title_key]
if str(p['price']) != str(old.get('price', '')):
changes.append(f'PRICE: {title_key[:35]} {old["price"]} -> {p["price"]}')
if p['position'] != old.get('position'):
changes.append(f'RANK: {title_key[:35]} #{old["position"]} -> #{p["position"]}')
return changes
for query in MONITOR_QUERIES:
products = search_amazon(query)
changes = detect_changes(query, products)
save_snapshot(query, products)
if changes:
print(f'\nChanges for "{query}":')
for c in changes:
print(f' {c}')
else:
print(f'No changes for "{query}"')Étape 3: Générer des alertes de surveillance
Créer des alertes lorsque des baisses de prix significatives ou des changements de classement se produisent.
def generate_alerts(monitor_queries):
alerts = []
for query in monitor_queries:
products = search_amazon(query)
changes = detect_changes(query, products)
save_snapshot(query, products)
for change in changes:
if 'PRICE' in change:
alerts.append({'type': 'price', 'query': query, 'detail': change})
elif 'RANK' in change:
alerts.append({'type': 'rank', 'query': query, 'detail': change})
print(f'\n=== Amazon Monitoring Report ===')
print(f' Date: {datetime.now().strftime("%Y-%m-%d")}')
print(f' Queries monitored: {len(monitor_queries)}')
print(f' Alerts: {len(alerts)}')
if alerts:
price_alerts = [a for a in alerts if a['type'] == 'price']
rank_alerts = [a for a in alerts if a['type'] == 'rank']
if price_alerts:
print(f'\n Price Alerts ({len(price_alerts)}):')
for a in price_alerts:
print(f' {a["detail"]}')
if rank_alerts:
print(f'\n Rank Alerts ({len(rank_alerts)}):')
for a in rank_alerts:
print(f' {a["detail"]}')
else:
print(f' No changes detected.')
print(f'\n Cost: ${len(monitor_queries) * 0.005:.3f}/scan')
print(f' Daily: ${len(monitor_queries) * 0.005:.3f}')
print(f' Monthly: ${len(monitor_queries) * 0.005 * 30:.2f}')
print(f' No scrapers. No proxies. No maintenance.')
generate_alerts(MONITOR_QUERIES)Exemple Python
import os, requests
SH = {'x-api-key': os.environ['SCAVIO_API_KEY'], 'Content-Type': 'application/json'}
def amazon_search(query):
data = requests.post('https://api.scavio.dev/api/v1/search',
headers=SH, json={'query': query, 'platform': 'amazon', 'country_code': 'us'}, timeout=10).json()
for r in data.get('organic_results', [])[:3]:
print(f'{r.get("title", "")[:45]} | {r.get("price", "N/A")}')
amazon_search('wireless earbuds')
print('Cost: $0.005')Exemple JavaScript
const SH = { 'x-api-key': process.env.SCAVIO_API_KEY, 'Content-Type': 'application/json' };
const data = await fetch('https://api.scavio.dev/api/v1/search', {
method: 'POST', headers: SH,
body: JSON.stringify({ query: 'wireless earbuds', platform: 'amazon', country_code: 'us' })
}).then(r => r.json());
(data.organic_results || []).slice(0, 3).forEach(r => {
console.log(`${r.title?.slice(0, 45)} | ${r.price || 'N/A'}`);
});Sortie attendue
wireless bluetooth earbuds: 10 products
#1 Apple AirPods Pro 2nd Generation - USB-C
Price: $189.99 | Rating: 4.7 | Reviews: 125,432
#2 Samsung Galaxy Buds3 Pro - AI Noise Cancel
Price: $159.99 | Rating: 4.5 | Reviews: 45,210
mechanical keyboard gaming: 10 products
#1 Keychron K8 Pro Wireless Mechanical Keyboard
Price: $109.00 | Rating: 4.6 | Reviews: 8,320
=== Amazon Monitoring Report ===
Queries monitored: 3
Alerts: 2
Price Alerts (1):
PRICE: Samsung Galaxy Buds3 Pro $169.99 -> $159.99
Cost: $0.015/scan
Monthly: $0.45