-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_asin_data.py
56 lines (44 loc) · 1.3 KB
/
get_asin_data.py
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
from pathlib import Path
from dotenv import load_dotenv
from os import getenv
import requests
import json
# Load environment variables
load_dotenv()
api_key = getenv('ASIN_API_KEY')
base_url = 'https://api.asindataapi.com/request'
term = 'gaming-laptops'
# Parameters for the API request
params = {
'api_key': api_key,
'type': 'search',
'amazon_domain': 'amazon.com',
'search_term': term,
'output': 'json',
}
# Send the API request
response = requests.get(base_url, params=params)
# Check if the request was successful
if response.status_code > 399:
print(f"Failed to retrieve data: {response.status_code}")
exit()
products = response.json().get('search_results', [])
print(f"Found {len(products)} items")
data = []
for product in products:
fetched_product = {
'title': product.get('title'),
'asin': product.get('asin'),
'image': product.get('image'),
'rating': product.get('rating'),
'rating_total': product.get('ratings_total'),
'price': product.get('price', {}).get('raw'),
'category': 'gaming-laptops',
}
data.append(fetched_product)
data_dir = Path('./data')
if not data_dir.exists():
data_dir.mkdir()
json_file = data_dir / f'/list_{term}.json'
with open(json_file, 'w') as file:
json.dump([], file, indent=4)