AxioAPI

Theo dõi giá đối thủ trên Shopify bằng Python

2026-10-05

Cửa hàng Shopify công khai đều công bố catalog sản phẩm. API catalog Shopify trả về nó dưới dạng JSON, nên bạn theo dõi giá mà không phải tự duy trì scraper. Hướng dẫn này xây một script nhỏ phát hiện thay đổi giá.

API trả về gì

GET /api/v1/ecommerce/shopify/catalog nhận tên miền, limit tùy chọn tối đa 250 và số trang. Mỗi sản phẩm gồm các biến thể với giá, giá gốc, tình trạng còn hàng và SKU.

Script

Script duyệt qua catalog, lưu snapshot giá biến thể vào file JSON và in ra mọi biến thể có giá khác với lần chạy trước.

import json
import os
import pathlib

import requests

API = "https://axioapi.com/api/v1/ecommerce/shopify/catalog"
HEADERS = {"Authorization": f"Bearer {os.environ['AXIOAPI_KEY']}"}


def fetch_prices(domain):
    prices, page = {}, 1
    while True:
        response = requests.get(API, headers=HEADERS, timeout=60,
                                params={"domain": domain, "limit": 250, "page": page})
        response.raise_for_status()
        products = response.json()["data"]["products"]
        for product in products:
            for variant in product["variants"]:
                prices[str(variant["id"])] = {
                    "name": f"{product['title']} / {variant['title']}",
                    "price": variant["price"],
                    "available": variant["available"],
                }
        if len(products) < 250:
            return prices
        page += 1


def main(domain):
    snapshot = pathlib.Path(f"{domain}.json")
    current = fetch_prices(domain)
    previous = json.loads(snapshot.read_text()) if snapshot.exists() else {}

    for variant_id, item in current.items():
        old = previous.get(variant_id)
        if old and old["price"] != item["price"]:
            print(f"{item['name']}: {old['price']} -> {item['price']}")

    snapshot.write_text(json.dumps(current))


if __name__ == "__main__":
    main("example.com")

Nên chạy bao lâu một lần

Kết quả của cùng một yêu cầu có thể được cache tối đa hai giờ, nên chạy thường xuyên hơn mỗi vài giờ sẽ không thấy dữ liệu mới hơn. Lịch chạy mỗi ngày hoặc mỗi giờ thường đủ để theo dõi giá.

Giới hạn và lưu ý

  • Chỉ hoạt động với cửa hàng Shopify công khai. Cửa hàng ẩn catalog sẽ trả lỗi.
  • Catalog lớn cần nhiều trang; mỗi trang là một yêu cầu tính phí.
  • Hãy tuân thủ điều khoản của cửa hàng và luật áp dụng với bạn khi dùng dữ liệu đối thủ.