Repository navigation
Expand file tree
/
Copy pathexample.py
More file actions
79 lines (66 loc) · 3.23 KB
/
Copy pathexample.py
File metadata and controls
79 lines (66 loc) · 3.23 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""DHgate scraper example: keywords -> dhgate_products.csv + a dropshipping shortlist.
Usage:
pip install -r requirements.txt
set APIFY_TOKEN=<YOUR_APIFY_TOKEN> (Windows) / export APIFY_TOKEN=... (macOS/Linux)
python example.py "led light" "yoga mat"
"""
import csv
import os
import statistics
import sys
from collections import Counter
from apify_client import ApifyClient
ACTOR_ID = "rel8ble/dhgate-scraper"
MAX_PER_KEYWORD = 100 # you pay per product saved ($0.50 per 1,000)
def run(keywords):
client = ApifyClient(os.environ.get("APIFY_TOKEN", "<YOUR_APIFY_TOKEN>"))
run_info = client.actor(ACTOR_ID).call(run_input={
"searchTerms": keywords,
"maxResultsPerSearch": MAX_PER_KEYWORD,
"includeAds": True,
})
return list(client.dataset(run_info["defaultDatasetId"]).iterate_items())
def write_csv(items, path="dhgate_products.csv"):
cols = ["searchTerm", "position", "productId", "title", "priceMin", "priceMax",
"priceUnit", "originalPriceMin", "discountPercent", "promotionEnds",
"moq", "moqUnit", "soldCount", "rating", "reviewCount", "freeShipping",
"isAd", "sellerName", "sellerPositiveFeedbackPercent", "url"]
with open(path, "w", newline="", encoding="utf-8") as f:
w = csv.DictWriter(f, fieldnames=cols, extrasaction="ignore")
w.writeheader()
w.writerows(items)
print(f"wrote {len(items)} rows to {path}")
def report(items):
by_kw = {}
for it in items:
by_kw.setdefault(it["searchTerm"], []).append(it)
for kw, rows in by_kw.items():
prices = [r["priceMin"] for r in rows if r.get("priceMin") is not None]
sellers = Counter(r.get("sellerName") for r in rows)
print(f"\n== {kw}: {len(rows)} products, {len(sellers)} sellers")
if prices:
print(f" median from-price ${statistics.median(prices):.2f} "
f"(range ${min(prices):.2f} - ${max(prices):.2f})")
print(f" MOQ 1: {sum(1 for r in rows if r.get('moq') == 1)} "
f"free shipping: {sum(1 for r in rows if r.get('freeShipping'))} "
f"on sale: {sum(1 for r in rows if r.get('discountPercent'))} "
f"sponsored: {sum(1 for r in rows if r.get('isAd'))}")
top = ", ".join(f"{s} ({n})" for s, n in sellers.most_common(3))
print(f" sellers with most listings: {top}")
# Dropshipping shortlist: single-unit MOQ, proven sales, trusted seller.
picks = [r for r in rows
if r.get("moq") == 1
and (r.get("soldCount") or 0) >= 10
and (r.get("rating") or 0) >= 4.5
and (r.get("sellerPositiveFeedbackPercent") or 0) >= 95]
picks.sort(key=lambda r: r.get("soldCount") or 0, reverse=True)
print(f" shortlist (MOQ 1, 10+ sold, 4.5+ stars, seller 95%+): {len(picks)}")
for r in picks[:5]:
sale = f" -{r['discountPercent']}%" if r.get("discountPercent") else ""
print(f" ${r['priceMin']:.2f}{sale} sold {r['soldCount']} "
f"{r['rating']}* {r['sellerName']} {r['title'][:60]}")
if __name__ == "__main__":
keywords = sys.argv[1:] or ["led light"]
items = run(keywords)
write_csv(items)
report(items)