Repository navigation
Expand file tree
/
Copy pathexample.py
More file actions
37 lines (30 loc) · 1.45 KB
/
Copy pathexample.py
File metadata and controls
37 lines (30 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""Scrape Alibaba.com products and suppliers with Python via the Alibaba Product Search Scraper on Apify.
pip install -r requirements.txt
set APIFY_TOKEN in your environment (or paste it below)
python example.py
"""
import csv
import os
from apify_client import ApifyClient
client = ApifyClient(os.environ.get("APIFY_TOKEN", "<YOUR_APIFY_TOKEN>"))
run_input = {
"searchTerms": ["yoga mat", "custom tote bags"],
"maxResultsPerSearch": 20, # you pay per product, keep it small while testing
"maxMoq": 100, # low-MOQ suppliers only; remove for all
"verifiedSuppliersOnly": True,
"includeAds": False,
}
run = client.actor("rel8ble/alibaba-product-search-scraper").call(run_input=run_input)
products = list(client.dataset(run["defaultDatasetId"]).iterate_items())
products.sort(key=lambda p: (p.get("priceMin") is None, p.get("priceMin") or 0))
print(f"Got {len(products)} products")
fields = ["searchTerm", "title", "priceMin", "priceMax", "moq", "moqUnit", "soldCount",
"leadTimeDays", "supplierName", "supplierCountryCode", "supplierYears",
"supplierRating", "responseRate", "url"]
with open("alibaba_products.csv", "w", newline="", encoding="utf-8") as f:
w = csv.DictWriter(f, fieldnames=fields, extrasaction="ignore")
w.writeheader()
w.writerows(products)
for p in products[:5]:
print(f"${p['priceMin']} MOQ {p['moq']} {p['supplierName']} {p['title'][:60]}")
print("Saved alibaba_products.csv")