forked from alricdsouza11/Optipick
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
57 lines (48 loc) · 1.99 KB
/
scraper.py
File metadata and controls
57 lines (48 loc) · 1.99 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
# scraper.py
import os
from apify_client import ApifyClient
APIFY_TOKEN = os.getenv("APIFY_TOKEN")
if not APIFY_TOKEN:
raise ValueError("APIFY_TOKEN environment variable is required")
ACTOR_ID = os.getenv("APIFY_ACTOR_ID")
if not ACTOR_ID:
raise ValueError("APIFY_ACTOR_ID environment variable is required")
_client = ApifyClient(APIFY_TOKEN)
def scrape_product(url: str, max_reviews: int = 100):
"""Fetch product details + reviews with your working Apify actor."""
run_input = {
"productUrls": [{"url": url}],
"maxReviews": int(max_reviews),
"scrapeProductDetails": True,
"sort": "helpful",
"filterByRatings": ["allStars"],
}
run = _client.actor(ACTOR_ID).call(run_input=run_input)
product, reviews = None, []
for item in _client.dataset(run["defaultDatasetId"]).iterate_items():
# product details
if not product and ("productTitle" in item or "productAsin" in item):
product = {
"asin": item.get("productAsin"),
"title": item.get("productTitle") or "N/A",
"price": item.get("productPrice") or "N/A",
"stars": item.get("productStars") or "N/A",
"image": item.get("productMainImageUrl"),
"url": url,
}
# reviews
if item.get("reviewDescription"):
reviews.append({
"asin": item.get("productAsin"),
"rating": item.get("ratingScore"),
"title": item.get("reviewTitle"),
"review": item.get("reviewDescription"),
"date": item.get("date"),
"country": item.get("country"),
"verified": item.get("isVerified"),
"url": item.get("reviewUrl"),
})
# Fallback product shell if Apify didn’t return details
if not product:
product = {"asin": None, "title": "N/A", "price": "N/A", "stars": "N/A", "image": None, "url": url}
return product, reviews